Last active
April 30, 2020 06:18
Revisions
-
oldrev revised this gist
Sep 27, 2019 . 1 changed file with 11 additions and 8 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 @@ -6,7 +6,7 @@ 本代码基于 BSD License 授权。 ''' from io import StringIO import math _RMB_DIGITS = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' ] @@ -15,9 +15,9 @@ def to_rmb_upper(price): price = round(price, 2) integer_part = int(price) wanyi_part = integer_part // 1000000000000 yi_part = integer_part % 1000000000000 // 100000000 wan_part = integer_part % 100000000 // 10000 qian_part = integer_part % 10000 dec_part = int(price * 100 % 100) @@ -65,7 +65,7 @@ def _parse_integer(strio, value, zero_count = 0, is_first_section = False): ndigits = int(math.floor(math.log10(value))) + 1 if value < 1000 and not is_first_section: zero_count += 1 for i in range(0, ndigits): factor = int(pow(10, ndigits - 1 - i)) digit = int(value / factor) if digit != 0: @@ -76,12 +76,12 @@ def _parse_integer(strio, value, zero_count = 0, is_first_section = False): zero_count = 0 else: zero_count += 1 value -= value // factor * factor return zero_count def _parse_decimal(strio, integer_part, value, zero_count): assert value > 0 and value <= 99 jiao = value // 10 fen = value % 10 if zero_count > 0 and (jiao > 0 or fen > 0) and integer_part > 0: strio.write('零') @@ -94,4 +94,7 @@ def _parse_decimal(strio, integer_part, value, zero_count): strio.write(_RMB_DIGITS[fen]) strio.write('分') else: strio.write('整') x = to_rmb_upper(223321.0) print(x) -
oldrev revised this gist
Sep 20, 2013 . 1 changed file with 4 additions and 5 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 @@ -25,18 +25,18 @@ def to_rmb_upper(price): zero_count = 0 #处理万亿以上的部分 if integer_part >= 1000000000000 and wanyi_part > 0: zero_count = _parse_integer(strio, wanyi_part, zero_count, True) strio.write('万') #处理亿到千亿的部分 if integer_part >= 100000000 and yi_part > 0: is_first_section = integer_part >= 100000000 and integer_part < 1000000000000 zero_count = _parse_integer(strio, yi_part, zero_count, is_first_section) strio.write('亿') #处理万的部分 if integer_part >= 10000 and wan_part > 0: is_first_section = integer_part >= 1000 and integer_part < 10000000 zero_count = _parse_integer(strio, wan_part, zero_count, is_first_section) strio.write('万') @@ -94,5 +94,4 @@ def _parse_decimal(strio, integer_part, value, zero_count): strio.write(_RMB_DIGITS[fen]) strio.write('分') else: strio.write('整') -
oldrev revised this gist
Sep 20, 2013 . 1 changed file with 2 additions and 140 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 @@ -71,146 +71,7 @@ def _parse_integer(strio, value, zero_count = 0, is_first_section = False): if digit != 0: if zero_count > 0: strio.write('零') strio.write(_RMB_DIGITS[digit]) strio.write(_SECTION_CHARS[ndigits - i - 1]) zero_count = 0 else: @@ -234,3 +95,4 @@ def _parse_decimal(strio, integer_part, value, zero_count): strio.write('分') else: strio.write('整') -
oldrev revised this gist
Sep 20, 2013 . No changes.There are no files selected for viewing
-
oldrev revised this gist
Sep 20, 2013 . 1 changed file with 155 additions and 11 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 @@ -26,22 +26,27 @@ def to_rmb_upper(price): zero_count = 0 #处理万亿以上的部分 if integer_part >= 1000000000000: zero_count = _parse_integer(strio, wanyi_part, zero_count, True) strio.write('万') #处理亿到千亿的部分 if integer_part >= 100000000: is_first_section = integer_part >= 100000000 and integer_part < 1000000000000 zero_count = _parse_integer(strio, yi_part, zero_count, is_first_section) strio.write('亿') #处理万的部分 if integer_part >= 10000: is_first_section = integer_part >= 1000 and integer_part < 10000000 zero_count = _parse_integer(strio, wan_part, zero_count, is_first_section) strio.write('万') #处理千及以后的部分 if qian_part > 0: is_first_section = integer_part < 1000 zero_count = _parse_integer(strio, qian_part, zero_count, is_first_section) else: zero_count += 1 if integer_part > 0: strio.write('元') @@ -55,35 +60,174 @@ def to_rmb_upper(price): return strio.getvalue() def _parse_integer(strio, value, zero_count = 0, is_first_section = False): assert value > 0 and value <= 9999 ndigits = int(math.floor(math.log10(value))) + 1 if value < 1000 and not is_first_section: zero_count += 1 for i in xrange(0, ndigits): factor = int(pow(10, ndigits - 1 - i)) digit = int(value / factor) if digit != 0: if zero_count > 0: strio.write('零') strio.write(_/** 中文互联网上迄今为止实现最正确、代码最漂亮且效率最高的大写人民币金额转换代码 * 作者:李维 <oldrev@gmail.com> * 版权所有 (c) 2013 昆明维智众源企业管理咨询有限公司。保留所有权利。 * 本代码基于 BSD License 授权。 * */ using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespace Sandwych.RmbConverter { public static class RmbUpperConverter { private static readonly Char[] RmbDigits = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; private static readonly string[] SectionChars = { string.Empty, "拾", "佰", "仟", "万" }; public static string ToRmbUpper(this decimal price) { if (price < 0M || price >= 9999999999999999.99M) { throw new ArgumentOutOfRangeException("price"); } price = Math.Round(price, 2); var sb = new StringBuilder(); var integerPart = (long)price; var wanyiPart = integerPart / 1000000000000L; var yiPart = integerPart % 1000000000000L / 100000000L; var wanPart = integerPart % 100000000L / 10000L; var qianPart = integerPart % 10000L; var decPart = (long)(price * 100) % 100; int zeroCount = 0; //处理万亿以上的部分 if (integerPart >= 1000000000000L) { zeroCount = ParseInteger(sb, wanyiPart, true, zeroCount); sb.Append("万"); } //处理亿到千亿的部分 if (integerPart >= 100000000L) { var isFirstSection = integerPart >= 100000000L && integerPart < 1000000000000L; zeroCount = ParseInteger(sb, yiPart, isFirstSection, zeroCount); sb.Append("亿"); } //处理万的部分 if (integerPart >= 10000L) { var isFirstSection = integerPart >= 1000L && integerPart < 10000000L; zeroCount = ParseInteger(sb, wanPart, isFirstSection, zeroCount); sb.Append("万"); } //处理千及以后的部分 if (qianPart > 0) { var isFirstSection = integerPart < 1000L; zeroCount = ParseInteger(sb, qianPart, isFirstSection, zeroCount); } else { zeroCount += 1; } if (integerPart > 0) { sb.Append("元"); } //处理小数 if (decPart > 0) { ParseDecimal(sb, integerPart, decPart, zeroCount); } else if (decPart <= 0 && integerPart > 0) { sb.Append("整"); } else { sb.Append("零元整"); } return sb.ToString(); } private static void ParseDecimal(StringBuilder sb, long integerPart, long decPart, int zeroCount) { Debug.Assert(decPart > 0 && decPart <= 99); var jiao = decPart / 10; var fen = decPart % 10; if (zeroCount > 0 && (jiao > 0 || fen > 0) && integerPart > 0) { sb.Append("零"); } if (jiao > 0) { sb.Append(RmbDigits[jiao]); sb.Append("角"); } if (zeroCount == 0 && jiao == 0 && fen > 0 && integerPart > 0) { sb.Append("零"); } if (fen > 0) { sb.Append(RmbDigits[fen]); sb.Append("分"); } else { sb.Append("整"); } } private static int ParseInteger(StringBuilder sb, long integer, bool isFirstSection, int zeroCount) { Debug.Assert(integer > 0 && integer <= 9999); int nDigits = (int)Math.Floor(Math.Log10(integer)) + 1; if (!isFirstSection && integer < 1000) { zeroCount++; } for (var i = 0; i < nDigits; i++) { var factor = (long)Math.Pow(10, nDigits - 1 - i); var digit = integer / factor; if (digit != 0) { if (zeroCount > 0) { sb.Append("零"); } sb.Append(RmbDigits[digit]); sb.Append(SectionChars[nDigits - i - 1]); zeroCount = 0; } else { if (i < nDigits) { zeroCount++; } } integer -= integer / factor * factor; } return zeroCount; } } }RMB_DIGITS[digit]) strio.write(_SECTION_CHARS[ndigits - i - 1]) zero_count = 0 else: zero_count += 1 value -= value / factor * factor return zero_count def _parse_decimal(strio, integer_part, value, zero_count): assert value > 0 and value <= 99 jiao = value / 10 fen = value % 10 if zero_count > 0 and (jiao > 0 or fen > 0) and integer_part > 0: strio.write('零') if jiao > 0: strio.write(_RMB_DIGITS[jiao]) strio.write('角') if zero_count == 0 and jiao == 0 and fen > 0 and integer_part > 0: strio.write('零') if fen > 0: strio.write(_RMB_DIGITS[fen]) -
oldrev revised this gist
Sep 20, 2013 . 1 changed file with 13 additions and 16 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 @@ -1,6 +1,6 @@ #encoding: utf-8 ''' 中文互联网上迄今为止实现最正确、代码最漂亮且效率最高的大写人民币金额转换代码 作者:李维 <[email protected]> 版权所有 (c) 2013 李维。保留所有权利。 本代码基于 BSD License 授权。 @@ -23,51 +23,45 @@ def to_rmb_upper(price): strio = StringIO() zero_count = 0 #处理万亿以上的部分 if integer_part >= 1000000000000: zero_count = _parse_integer(strio, wanyi_part, zero_count) strio.write('万') #处理亿到千亿的部分 if integer_part >= 100000000: zero_count = _parse_integer(strio, yi_part, zero_count) strio.write('亿') #处理万的部分 if integer_part >= 10000: zero_count = _parse_integer(strio, wan_part, zero_count) strio.write('万') #处理千及以后的部分 if qian_part > 0: zero_count = _parse_integer(strio, qian_part, zero_count) if integer_part > 0: strio.write('元') #处理小数 if dec_part > 0: _parse_decimal(strio, integer_part, dec_part, zero_count) elif dec_part == 0 and integer_part > 0: strio.write('整') else: strio.write('零元整') return strio.getvalue() def _parse_integer(strio, value, zero_count = 0): assert value > 0 and value <= 9999 ndigits = int(math.floor(math.log10(value))) + 1 #zero_count = 0 for i in xrange(0, ndigits): factor = int(pow(10, ndigits - 1 - i)) digit = int(value / factor) if digit != 0: if zero_count > 0: strio.write('零') @@ -78,11 +72,14 @@ def _parse_integer(strio, value): if i < ndigits: zero_count += 1 value -= value / factor * factor return zero_count def _parse_decimal(strio, integer_part, value, zero_count): assert value > 0 and value <= 99 jiao = value / 10 fen = value % 10 if zero_count > 0 and (jiao > 0 or fen > 0): strio.write('零') if jiao > 0: strio.write(_RMB_DIGITS[jiao]) strio.write('角') -
oldrev revised this gist
Sep 20, 2013 . 1 changed file with 4 additions and 2 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 @@ -53,8 +53,10 @@ def to_rmb_upper(price): #处理小数 if dec_part > 0: _parse_decimal(strio, integer_part, dec_part) elif dec_part == 0 and integer_part > 0: strio.write('整') else: strio.write('零元整') return strio.getvalue() @@ -90,4 +92,4 @@ def _parse_decimal(strio, integer_part, value): strio.write(_RMB_DIGITS[fen]) strio.write('分') else: strio.write('整') -
oldrev revised this gist
Sep 20, 2013 . 1 changed file with 3 additions and 3 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 @@ -52,7 +52,7 @@ def to_rmb_upper(price): #处理小数 if dec_part > 0: _parse_decimal(strio, integer_part, dec_part) else: strio.write('整') @@ -77,14 +77,14 @@ def _parse_integer(strio, value): zero_count += 1 value -= value / factor * factor def _parse_decimal(strio, integer_part, value): assert value > 0 and value <= 99 jiao = value / 10 fen = value % 10 if jiao > 0: strio.write(_RMB_DIGITS[jiao]) strio.write('角') if jiao == 0 and fen > 0 and integer_part > 0: strio.write('零') if fen > 0: strio.write(_RMB_DIGITS[fen]) -
oldrev revised this gist
Sep 20, 2013 . 1 changed file with 12 additions and 13 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 @@ -14,36 +14,36 @@ def to_rmb_upper(price): price = round(price, 2) integer_part = int(price) wanyi_part = integer_part / 1000000000000 yi_part = integer_part % 1000000000000 / 100000000 wan_part = integer_part % 100000000 / 10000 qian_part = integer_part % 10000 dec_part = int(price * 100 % 100) strio = StringIO() #处理万亿以上的部分 if integer_part >= 1000000000000: _parse_integer(strio, wanyi_part) strio.write('万') #处理亿到千亿的部分 if integer_part >= 100000000: if integer_part >= 1000000000000 and yi_part > 0 and yi_part <= 999: strio.write('零') _parse_integer(strio, yi_part) strio.write('亿') #处理万的部分 if integer_part >= 10000: if integer_part >= 100000000 and wan_part > 0 and wan_part <= 999: strio.write('零') _parse_integer(strio, wan_part) strio.write('万') #处理千及以后的部分 if integer_part >= 10000 and qian_part > 0 and qian_part <= 999: strio.write('零'); if qian_part > 0: _parse_integer(strio, qian_part) @@ -90,5 +90,4 @@ def _parse_decimal(strio, value): strio.write(_RMB_DIGITS[fen]) strio.write('分') else: strio.write('整') -
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,94 @@ #encoding: utf-8 ''' 中文互联网上写的最漂亮效率最高的大写人民币金额转换代码 作者:李维 <[email protected]> 版权所有 (c) 2013 李维。保留所有权利。 本代码基于 BSD License 授权。 ''' from cStringIO import StringIO import math _RMB_DIGITS = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' ] _SECTION_CHARS = ['', '拾', '佰', '仟', '万' ] def to_rmb_upper(price): price = round(price, 2) wanyi_part = int(price / 1000000000000) yi_part = int(price % 1000000000000 / 100000000) wan_part = int(price % 100000000 / 10000) qian_part = int(price % 10000) integer_part = int(price) dec_part = int((price * 100) % 100) strio = StringIO() #处理万亿以上的部分 if price >= 1000000000000.00: _parse_integer(strio, wanyi_part) strio.write('万') #处理亿到千亿的部分 if price >= 100000000.00: if price >= 1000000000000.00 and yi_part > 0 and yi_part <= 999: strio.write('零') _parse_integer(strio, yi_part) strio.write('亿') #处理万的部分 if price >= 10000.00: if price >= 100000000.00 and wan_part > 0 and wan_part <= 999: strio.write('零') _parse_integer(strio, wan_part) strio.write('万') #处理千及以后的部分 if price >= 10000.00 and qian_part > 0 and qian_part <= 999: strio.write('零'); if qian_part > 0: _parse_integer(strio, qian_part) if integer_part > 0: strio.write('元') #处理小数 if dec_part > 0: _parse_decimal(strio, dec_part) else: strio.write('整') return strio.getvalue() def _parse_integer(strio, value): assert value > 0 and value <= 9999 ndigits = int(math.floor(math.log10(value))) + 1 zero_count = 0 for i in xrange(0, ndigits): factor = int(pow(10, ndigits - 1 - i)) digit = int(value / factor) if digit != 0: if zero_count > 0: strio.write('零') strio.write(_RMB_DIGITS[digit]) strio.write(_SECTION_CHARS[ndigits - i - 1]) zero_count = 0 else: if i < ndigits: zero_count += 1 value -= value / factor * factor def _parse_decimal(strio, value): assert value > 0 and value <= 99 jiao = value / 10 fen = value % 10 if jiao > 0: strio.write(_RMB_DIGITS[jiao]) strio.write('角') if jiao == 0 and fen > 0: strio.write('零') if fen > 0: strio.write(_RMB_DIGITS[fen]) strio.write('分') else: strio.write('整')