这就防止财务 库管 客户做手脚,涉及到钱这个问题是很现实的
我建议如果你是要打印,你也要封死前面和后面
我建议如果你是要打印,你也要封死前面和后面
public static string FormatAmount(string amount) { //定义 string str = ""; char[] TOBIG = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; char[] TOUNIT = { '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万' }; char[] TOUNITDE = { '角', '分', '厘', '毫', '丝' }; string amount1 = ""; string amount2 = ""; int dot = amount.IndexOf('.'); int start = 0; //获取非0的开始位置 for (int i = 0; i <= dot; i++) if (amount[i] == '0') start++; else break; //把数字分段 if (dot == -1) { dot = amount.Length; amount1 = amount.Substring(0, dot); } else { if (start == 0) amount1 = amount.Substring(start, dot); else { int length = dot - start; amount1 = amount.Substring(start, length); } amount2 = amount.Substring(++dot, amount.Length - dot); } //转化整数位 int n = 0; for (int i = 0; i < amount1.Length; i++) { int j = amount1.Length - 1 - i; n = i; if (amount1[i] == '0') { for (int k = i; k < amount1.Length - 1; k++) { if (amount1[i + 1] == '0') i++; else break; } if (i != n || (amount1.Length - n - 1) % 4 == 0) { if (j >= 8) str += TOUNIT[8].ToString(); else if (j >= 4) str += TOUNIT[4].ToString(); else if (amount1.Length == 1 || i != amount1.Length - 1) str += ""; } else str += TOBIG[0].ToString(); } else { str += TOBIG[int.Parse(amount1[i].ToString())].ToString(); str += j == 0 ? "" : TOUNIT[j].ToString(); } } if (amount1.Length != 0) str += TOUNIT[0].ToString(); if (amount2.Length == 0) str += "整"; //转化小数位 if (dot != amount.Length) { int flag = 0; for (int i = 0; i < amount2.Length; i++) { if (amount2[i] == '0') { for (int k = i; k < amount2.Length - 1; k++) if (amount2[i + 1] == '0') i++; else break; if (i == amount2.Length - 1 && flag == 0 && amount1.Length != 0) str += "整"; else if (amount1.Length == 0 && flag == 0) str += TOBIG[0].ToString() + TOUNIT[0].ToString(); else if (amount1.Length == 0 || i == amount2.Length - 1) str += ""; else str += TOBIG[0].ToString(); } else { flag = 1; str += TOBIG[int.Parse(amount2[i].ToString())].ToString(); str += i > 4 ? "" : TOUNITDE[i].ToString(); } } } return str; }