Skip to content

Instantly share code, notes, and snippets.

@e-mihaylin
Last active June 19, 2018 12:14
Show Gist options
  • Save e-mihaylin/8865031be0d1d1e2e95ae41a4400207b to your computer and use it in GitHub Desktop.
Save e-mihaylin/8865031be0d1d1e2e95ae41a4400207b to your computer and use it in GitHub Desktop.
function toChineseNumeral(num) {
var numerals = {
"-":"负",
".":"点",
0:"零",
1:"一",
2:"二",
3:"三",
4:"四",
5:"五",
6:"六",
7:"七",
8:"八",
9:"九",
10:"十",
100:"百",
1000:"千",
10000:"万"
};
if (num < 0) {
return numerals['-'] + toChineseNumeral(-num);
} else if (num < 1) {
return num.toString().split('').reduce(function(p, n) {
return p + numerals[n];
}, '');
} else if (num > Math.floor(num)) {
return toChineseNumeral(Math.floor(num)) + toChineseNumeral(parseFloat(num.toString().replace(/^.*\./, '0.'))).slice(1);
} else {
return [10000, 1000, 100, 10, 1].reduce(function(p, n) {
if (num >= n) {
if (p.ling) p.ch += numerals[0];
p.ch += numerals[Math.floor(num / n)];
if (n != 1) p.ch += numerals[n];
p.ling = false;
} else if (p.ch) {
p.ling = true;
}
num %= n;
return p;
}, {ch: '', ling: false}).ch.replace(/^一十/, '十');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment