服务器之家

服务器之家 > 正文

C#实现金额转换成中文大写金额

时间:2022-02-28 13:28     来源/作者:i_mengli

本文实例为大家分享了C#金额转换成中文大写金额的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/// <summary>
  /// 金额转换成中文大写金额
  /// </summary>
  /// <param name="LowerMoney">eg:10.74</param>
  /// <returns></returns>
  public static string MoneyToUpper(string LowerMoney)
  {
   string functionReturnValue = null;
   bool IsNegative = false; // 是否是负数
   if (LowerMoney.Trim().Substring(0, 1) == "-")
   {
    // 是负数则先转为正数
    LowerMoney = LowerMoney.Trim().Remove(0, 1);
    IsNegative = true;
   }
   string strLower = null;
   string strUpart = null;
   string strUpper = null;
   int iTemp = 0;
   // 保留两位小数 123.489→123.49  123.4→123.4
   LowerMoney = Math.Round(double.Parse(LowerMoney), 2).ToString();
   if (LowerMoney.IndexOf(".") > 0)
   {
    if (LowerMoney.IndexOf(".") == LowerMoney.Length - 2)
    {
     LowerMoney = LowerMoney + "0";
    }
   }
   else
   {
    LowerMoney = LowerMoney + ".00";
   }
   strLower = LowerMoney;
   iTemp = 1;
   strUpper = "";
   while (iTemp <= strLower.Length)
   {
    switch (strLower.Substring(strLower.Length - iTemp, 1))
    {
     case ".":
      strUpart = "圆";
      break;
     case "0":
      strUpart = "零";
      break;
     case "1":
      strUpart = "壹";
      break;
     case "2":
      strUpart = "贰";
      break;
     case "3":
      strUpart = "叁";
      break;
     case "4":
      strUpart = "肆";
      break;
     case "5":
      strUpart = "伍";
      break;
     case "6":
      strUpart = "陆";
      break;
     case "7":
      strUpart = "柒";
      break;
     case "8":
      strUpart = "捌";
      break;
     case "9":
      strUpart = "玖";
      break;
    }
 
    switch (iTemp)
    {
     case 1:
      strUpart = strUpart + "分";
      break;
     case 2:
      strUpart = strUpart + "角";
      break;
     case 3:
      strUpart = strUpart + "";
      break;
     case 4:
      strUpart = strUpart + "";
      break;
     case 5:
      strUpart = strUpart + "拾";
      break;
     case 6:
      strUpart = strUpart + "佰";
      break;
     case 7:
      strUpart = strUpart + "仟";
      break;
     case 8:
      strUpart = strUpart + "万";
      break;
     case 9:
      strUpart = strUpart + "拾";
      break;
     case 10:
      strUpart = strUpart + "佰";
      break;
     case 11:
      strUpart = strUpart + "仟";
      break;
     case 12:
      strUpart = strUpart + "亿";
      break;
     case 13:
      strUpart = strUpart + "拾";
      break;
     case 14:
      strUpart = strUpart + "佰";
      break;
     case 15:
      strUpart = strUpart + "仟";
      break;
     case 16:
      strUpart = strUpart + "万";
      break;
     default:
      strUpart = strUpart + "";
      break;
    }
 
    strUpper = strUpart + strUpper;
    iTemp = iTemp + 1;
   }
 
   strUpper = strUpper.Replace("零拾", "零");
   strUpper = strUpper.Replace("零佰", "零");
   strUpper = strUpper.Replace("零仟", "零");
   strUpper = strUpper.Replace("零零零", "零");
   strUpper = strUpper.Replace("零零", "零");
   strUpper = strUpper.Replace("零角零分", "整");
   strUpper = strUpper.Replace("零分", "整");
   strUpper = strUpper.Replace("零角", "零");
   strUpper = strUpper.Replace("零亿零万零圆", "亿圆");
   strUpper = strUpper.Replace("亿零万零圆", "亿圆");
   strUpper = strUpper.Replace("零亿零万", "亿");
   strUpper = strUpper.Replace("零万零圆", "万圆");
   strUpper = strUpper.Replace("零亿", "亿");
   strUpper = strUpper.Replace("零万", "万");
   strUpper = strUpper.Replace("零圆", "圆");
   strUpper = strUpper.Replace("零零", "零");
 
   // 对壹圆以下的金额的处理
   if (strUpper.Substring(0, 1) == "圆")
   {
    strUpper = strUpper.Substring(1, strUpper.Length - 1);
   }
   if (strUpper.Substring(0, 1) == "零")
   {
    strUpper = strUpper.Substring(1, strUpper.Length - 1);
   }
   if (strUpper.Substring(0, 1) == "角")
   {
    strUpper = strUpper.Substring(1, strUpper.Length - 1);
   }
   if (strUpper.Substring(0, 1) == "分")
   {
    strUpper = strUpper.Substring(1, strUpper.Length - 1);
   }
   if (strUpper.Substring(0, 1) == "整")
   {
    strUpper = "零圆整";
   }
   functionReturnValue = strUpper;
 
   if (IsNegative == true)
   {
    return "负" + functionReturnValue;
   }
   else
   {
    return functionReturnValue;
   }
 }

decimal PriceSum = 32957.2654;

调用  var PriceSumChinese = MoneyToUpper(PriceSum.ToString());

结果:叁万贰仟玖佰伍拾柒圆贰角柒分

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/i-mengli/archive/2018/08/24/9530329.html

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部