-
-
Save ccdos/6633891 to your computer and use it in GitHub Desktop.
Revisions
-
oldrev revised this gist
Sep 20, 2013 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,6 @@ public class RmbUpperConverter { public static string ToUpper(double price) { var sb = new StringBuilder(); var qianPart = (long)(price % 10000); var wanPart = ((long)(price % 100000000) - qianPart) / 10000; var yiPart = ((long)(price % 100000000000) - qianPart - wanPart) / 100000000; -
oldrev revised this gist
Sep 20, 2013 . 1 changed file with 23 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,10 +13,11 @@ namespace RMBUpper { public class RmbUpperConverter { private static readonly Char[] RmbDigits = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; private static readonly string[] DecDigits = { string.Empty, "拾", "佰", "仟", string.Empty, "万" }; public static string ToUpper(double price) { var sb = new StringBuilder(); @@ -56,28 +57,26 @@ public static string ToUpper(double price) { } private static void ParseDecimal(StringBuilder sb, long decPart) { if (decPart > 0) { var jiao = decPart / 10; var fen = decPart % 10; if (jiao > 0) { sb.Append(RmbDigits[jiao]); sb.Append("角"); } if (jiao == 0 && fen > 0) { sb.Append("零"); } if (fen > 0) { sb.Append(RmbDigits[fen]); sb.Append("分"); } else { sb.Append("整"); } } else { sb.Append("整"); } } @@ -109,9 +108,7 @@ private static void ParseInteger(StringBuilder sb, long value) { if (!skipSpec) { var f1 = nDigits - i - 1; sb.Append(DecDigits[f1]); } u -= u / factor * factor; -
oldrev created this gist
Sep 20, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,124 @@ /** 中文互联网上写的最漂亮效率最高的大写人民币金额转换代码 * 作者:李维 <[email protected]> * 版权所有 (c) 2013 李维。保留所有权利。 * 本代码基于 BSD License 授权。 * */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace RMBUpper { public class RmbUpperConverter { private static readonly Char[] RmbDigits = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; private static readonly Dictionary<int, string> DecDigits = new Dictionary<int, string>() { { 0, "" }, { 1, "拾" }, { 2, "佰" }, { 3, "仟" }, { 5, "万" } }; public static string ToUpper(double price) { var sb = new StringBuilder(); int nDigits = (int)Math.Floor(Math.Log10(Math.Abs(price))) + 1; var qianPart = (long)(price % 10000); var wanPart = ((long)(price % 100000000) - qianPart) / 10000; var yiPart = ((long)(price % 100000000000) - qianPart - wanPart) / 100000000; var integerPart = yiPart * 100000000 + wanPart * 10000 + qianPart; var decPart = (long)(price * 100) % 100; //处理亿的部分 if (price > 99999999.99) { ParseInteger(sb, yiPart); sb.Append("亿"); } //处理万的部分 if (price > 9999.99) { if (price > 99999999.99 && wanPart > 0 && wanPart <= 999) { sb.Append("零"); } ParseInteger(sb, wanPart); sb.Append("万"); } //处理千及以后的部分 if (price > 9999.99 && qianPart > 0 && qianPart <= 999) { sb.Append("零"); } ParseInteger(sb, qianPart); sb.Append("元"); ParseDecimal(sb, decPart); return sb.ToString(); } private static void ParseDecimal(StringBuilder sb, long decPart) { if (decPart <= 0) { sb.Append("整"); } else { if (decPart > 0) { var jiao = decPart / 10; var fen = decPart % 10; if (jiao > 0) { sb.Append(RmbDigits[jiao]); sb.Append("角"); } if (jiao == 0 && fen > 0) { sb.Append("零"); } if (fen > 0) { sb.Append(RmbDigits[fen]); sb.Append("分"); } else { sb.Append("整"); } } } } private static void ParseInteger(StringBuilder sb, long value) { var u = Math.Abs(value); int nDigits = (int)Math.Floor(Math.Log10(u)) + 1; long lastDigit = -1; for (var i = 0; i < nDigits; i++) { var factor = (long)Math.Pow(10, nDigits - 1 - i); var digit = u / factor; //几种情况 //遇到零先跳过,下次循环再处理 var skipSpec = false; if (digit == 0 && i < nDigits - 1) { skipSpec = true; } //最后一位是0,则不能转换为“零” else if (digit == 0 && i >= nDigits - 1) { skipSpec = false; }//连续两位都是 0,则只出一个零 else if (lastDigit == 0 && digit == 0 && i < nDigits - 1) { sb.Append(RmbDigits[digit]); skipSpec = true; } else { sb.Append(RmbDigits[digit]); skipSpec = false; } if (!skipSpec) { var f1 = nDigits - i - 1; if (DecDigits.ContainsKey(f1)) { sb.Append(DecDigits[f1]); } } u -= u / factor * factor; lastDigit = digit; } } } }