c# 金钱大写转小写工具类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TJCFinanceWriteOff.BizLogic.Common { /// <summary> /// C#金额大写转小写 /// </summary> public static class MoneyUtil { public static string RMBChineseConvertToNumber(string str) { if (string.IsNullOrEmpty(str)) { return string.Empty; } str = str.Replace("零", "").Replace("整", ""); var arr = str.ToCharArray(); double money = 0; //最终的金额 bool yi = false; bool wan = false; bool qian = false; bool bai = false; bool shi = false; bool yuan = false; bool jiao = false; bool fen = false; for (int i = (arr.Length - 1); i >= 0; i--) { double cur = 0; if (arr[i] == '分') { fen = true; continue; } else if (arr[i] == '角') { jiao = true; fen = false; continue; } else if (arr[i] == '元') { yuan = true; jiao = false; fen = false; continue; } else if (arr[i] == '拾') { shi = true; yuan = false; jiao = false; fen = false; continue; } else if (arr[i] == '佰') { bai = true; shi = false; yuan = false; jiao = false; fen = false; continue; } else if (arr[i] == '仟' || arr[i] == '任') { qian = true; bai = false; shi = false; yuan = false; jiao = false; fen = false; continue; } else if (arr[i] == '万') { wan = true; qian = false; bai = false; shi = false; yuan = false; jiao = false; fen = false; continue; } else if (arr[i] == '亿') { yi = true; wan = false; qian = false; bai = false; shi = false; yuan = false; jiao = false; fen = false; continue; } if (fen) cur = ConvertNameToSmall(arr[i]) * 0.01; else if (jiao) { cur = ConvertNameToSmall(arr[i]) * 0.1; jiao = false; } else if (shi) cur = ConvertNameToSmall(arr[i]) * 10; else if (bai) cur = ConvertNameToSmall(arr[i]) * 100; else if (qian) cur = ConvertNameToSmall(arr[i]) * 1000; else if (yuan) cur = ConvertNameToSmall(arr[i]) * 1; else { cur = ConvertNameToSmall(arr[i]); } if (yi) { cur = cur * 100000000; } else if (wan) { cur = cur * 10000; } else { // cur = cur; } money += cur; } return MoneyToString(money); } private static int ConvertNameToSmall(char str) { int name = 0; switch (str.ToString()) { case "零": name = 0; break; case "壹": name = 1; break; case "贰": name = 2; break; case "叁": name = 3; break; case "肆": name = 4; break; case "伍": name = 5; break; case "陆": name = 6; break; case "柒": name = 7; break; case "捌": name = 8; break; case "玖": name = 9; break; default: break; } return name; } public static string MoneyToString(double money) { var moneyArray = money.ToString().ToList(); int index = moneyArray.IndexOf('.'); int integerLengthIndex = index != -1 ? index : moneyArray.Count; while (integerLengthIndex - 3 > 0) { moneyArray.Insert(integerLengthIndex - 3, ','); integerLengthIndex = moneyArray.IndexOf(','); } StringBuilder sb = new StringBuilder("¥"); sb.Append(string.Concat(moneyArray)); switch (moneyArray.IndexOf('.')) { case int i when i == moneyArray.Count - 2: sb.Append("0"); break; case -1: sb.Append(".00"); break; default: break; } return sb.ToString(); } } }