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 | /// <summary> /// 转换为人民币大写金额形式 /// </summary> /// <param name="Money">金额</param> /// <returns>返回大写形式</returns> public static string NumberToChineseMoney( this object number) { decimal numberMoney = 0; try { numberMoney = Convert.ToDecimal(number); } catch { return "非数字形式!" ; } //0-9数字所对应的汉字 const string numericChinese = "零壹贰叁肆伍陆柒捌玖" ; //数字单位所对应的汉字 string unitChinese = "万仟佰拾亿仟佰拾万仟佰拾元角分" ; //将Money取绝对值并四舍五入取2位小数 numberMoney = Math.Round(Math.Abs(numberMoney), 2); //数字的字符串形式 string strNumeric = (( long )(numberMoney * 100)).ToString(); //人民币大写金额形式 string strCmycurD = "" ; //最高单位 int maxUnit = strNumeric.Length; if (maxUnit > 15) { return "溢出" ; } //取出对应位数的unitChinese的值。如:200.55,maxUnit为5所以unitChinese=佰拾元角分 unitChinese = unitChinese.Substring(15 - maxUnit); //数字单位的汉字转换 string unitConvert = "" ; //用来计算连续的零值是几个 int nzero = 0; //循环取出每一位需要转换的值 for ( int i = 0; i < maxUnit; i++) { //数字的汉字转换 string numericConvert; //取出需转换的某一位的值 string strConvertValue = strNumeric.Substring(i, 1); //转换为数字 int convertValue = Convert.ToInt32(strConvertValue); if (i != (maxUnit - 3) && i != (maxUnit - 7) && i != (maxUnit - 11) && i != (maxUnit - 15)) { //当所取位数不为元、万、亿、万亿上的数字时 if (strConvertValue == "0" ) { numericConvert = "" ; unitConvert = "" ; nzero = nzero + 1; } else { if (strConvertValue != "0" && nzero != 0) { numericConvert = "零" + numericChinese.Substring(convertValue * 1, 1); unitConvert = unitChinese.Substring(i, 1); nzero = 0; } else { numericConvert = numericChinese.Substring(convertValue * 1, 1); unitConvert = unitChinese.Substring(i, 1); nzero = 0; } } } else { //该位是万亿,亿,万,元位等关键位 if (strConvertValue != "0" && nzero != 0) { numericConvert = "零" + numericChinese.Substring(convertValue * 1, 1); unitConvert = unitChinese.Substring(i, 1); nzero = 0; } else { if (strConvertValue != "0" && nzero == 0) { numericConvert = numericChinese.Substring(convertValue * 1, 1); unitConvert = unitChinese.Substring(i, 1); nzero = 0; } else { if (strConvertValue == "0" && nzero >= 3) { numericConvert = "" ; unitConvert = "" ; nzero = nzero + 1; } else { if (maxUnit >= 11) { numericConvert = "" ; nzero = nzero + 1; } else { numericConvert = "" ; unitConvert = unitChinese.Substring(i, 1); nzero = nzero + 1; } } } } } if (i == (maxUnit - 11) || i == (maxUnit - 3)) { //如果该位是亿位或元位,则必须写上 unitConvert = unitChinese.Substring(i, 1); } strCmycurD = strCmycurD + numericConvert + unitConvert; if (i == maxUnit - 1 && strConvertValue == "0" ) { //最后一位(分)为0时,加上“整” strCmycurD = strCmycurD + '整' ; } } if (numberMoney == 0) { strCmycurD = "零元整" ; } return strCmycurD; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现