Created
July 21, 2016 14:45
-
-
Save loskael/d72834665f10f24dea4cc46181458134 to your computer and use it in GitHub Desktop.
number to chinese
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
function number_cn(num, def) { | |
def = def || 0; | |
var output = []; | |
var UNIT_STR = ['', '十', '百', '千', '万']; | |
var NUMBER_STR = '零一二三四五六七八九十'; | |
num = Math.max(parseInt(num, 10) || def, def).toString().split(''); | |
while (num.length) { | |
var val = parseInt(num.shift(), 10); | |
output.push(NUMBER_STR.charAt(val) + (val === 0 ? '' : UNIT_STR[num.length])); | |
} | |
return output.join('').replace(/(?:^一(十)|(零)零+|零+$)/g, '$1'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment