Last active
December 21, 2015 00:38
-
-
Save wonderbeyond/6221501 to your computer and use it in GitHub Desktop.
大写金额算法 - 转换数字金额为大写汉字格式
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 characters
| #!/usr/bin/env node | |
| function capital_money(value) { | |
| function get_tidy_value(value) { | |
| var s_value = value.toFixed(2); | |
| if(s_value.indexOf('.') >= 0) { | |
| var int_part = s_value.split('.')[0]; | |
| var float_part = s_value.split('.')[1]; | |
| } else { | |
| var int_part = s_value; | |
| var float_part = '0'; | |
| } | |
| int_part = ('000000000000' + int_part).replace(new RegExp('0{' + int_part.length + '}'), ''); | |
| float_part = ('00' + float_part).replace(new RegExp('0{' + float_part.length + '}'), ''); | |
| return int_part + '.' + float_part; | |
| } | |
| var num_map = '零壹贰叁肆伍陆柒捌玖'; | |
| var unit_map = [ | |
| ['仟', 4, '亿'], | |
| ['佰', 3, '亿'], | |
| ['拾', 2, '亿'], | |
| ['亿', 1, ''], | |
| ['仟', 4, '万'], | |
| ['佰', 3, '万'], | |
| ['拾', 2, '万'], | |
| ['万', 5, '元'], | |
| ['仟', 4, '元'], | |
| ['佰', 3, '元'], | |
| ['拾', 2, '元'], | |
| ['元', 1, '元'], | |
| ['零', 1, ''], | |
| ['角', 1, ''], | |
| ['分', 1, ''] | |
| ]; | |
| if(!value) { return ''; } | |
| if(isNaN(value)) { return '无效的金额'; } | |
| var tidy_value = get_tidy_value(parseFloat(value)); | |
| var buf = ''; | |
| for(var idx in tidy_value) { | |
| var c = tidy_value.charAt(idx); | |
| if(c == '.') { | |
| buf = buf.replace(/零+$/, '').replace(/元+$/, ''); | |
| if(buf.length > 0){ buf += '元'; } | |
| } | |
| if(c == '.' || c == '0') { | |
| if(buf.charAt(buf.length - 1) != '零') { | |
| buf += '零'; | |
| } | |
| continue; | |
| } else { | |
| buf += (num_map.charAt(parseInt(c)) + unit_map[idx][0]); | |
| if(unit_map[idx][1] > 1) { | |
| var follow_value = tidy_value.substr(+idx+1, unit_map[idx][1]-1); | |
| if(parseInt(follow_value) == 0) { | |
| buf += unit_map[idx][2]; | |
| } | |
| } | |
| } | |
| } | |
| return buf.replace(/^零+/, '').replace(/零+$/, ''); | |
| } | |
| if(typeof process != 'undefined'){ | |
| console.log(capital_money(process.argv[2])); | |
| } |
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 characters
| #!/usr/bin/python | |
| # coding=utf-8 | |
| def get_tidy_value(value): | |
| ''' | |
| >>> get_tidy_value(1) | |
| '000000000001.00' | |
| >>> get_tidy_value(1.8) | |
| '000000000001.80' | |
| >>> get_tidy_value(1.08) | |
| '000000000001.08' | |
| >>> get_tidy_value(17812.182) | |
| '000000017812.18' | |
| ''' | |
| s_value = repr( round(value, 2) ) | |
| if '.' in s_value: | |
| int_part,float_part = s_value.split('.') | |
| else: | |
| int_part,float_part = s_value, '0' | |
| return '%s.%s' % (int_part.rjust(12, '0'), float_part.ljust(2, '0')) | |
| def capital_money(value): | |
| num_map = [n for n in u'零壹贰叁肆伍陆柒捌玖'] | |
| unit_map = ( | |
| (u'仟', 4, u'亿'), | |
| (u'佰', 3, u'亿'), | |
| (u'拾', 2, u'亿'), | |
| (u'亿', 1, u''), | |
| (u'仟', 4, u'万'), #1KW | |
| (u'佰', 3, u'万'), #100W | |
| (u'拾', 2, u'万'), #10W | |
| (u'万', 5, u'元'), | |
| (u'仟', 4, u'元'), | |
| (u'佰', 3, u'元'), | |
| (u'拾', 2, u'元'), | |
| (u'元', 1, u'元'), | |
| (u'零', 1, u''), #小数点 | |
| (u'角', 1, u''), | |
| (u'分', 1, u''), | |
| ) | |
| tidy_value = get_tidy_value(float(value)) | |
| buf = '' | |
| for idx, c in enumerate(tidy_value): | |
| #检查整数位走完后有没有 '元' | |
| if c in ['.']: | |
| buf = buf.strip(u'零').strip(u'元') | |
| if len(buf) > 0: | |
| buf += u'元' | |
| if c in ['.', '0']: | |
| if not buf.endswith(u'零'): | |
| buf += u'零' | |
| continue | |
| else: | |
| buf += u'%s%s' % (num_map[int(c)], unit_map[idx][0]) | |
| if unit_map[idx][1] > 1: | |
| follow_value = tidy_value[idx : idx+unit_map[idx][1]][1:] | |
| if int(follow_value) == 0: | |
| buf += unit_map[idx][2] | |
| return buf.strip(u'零') | |
| if __name__ == '__main__': | |
| import doctest | |
| doctest.testmod() | |
| import sys | |
| print capital_money(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment