Skip to content

Instantly share code, notes, and snippets.

@loskael
Created July 21, 2016 14:45
Show Gist options
  • Save loskael/d72834665f10f24dea4cc46181458134 to your computer and use it in GitHub Desktop.
Save loskael/d72834665f10f24dea4cc46181458134 to your computer and use it in GitHub Desktop.
number to chinese
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