Skip to content

Instantly share code, notes, and snippets.

@zbinlin
Created December 2, 2014 13:54
Show Gist options
  • Save zbinlin/46406c39b4dea903a122 to your computer and use it in GitHub Desktop.
Save zbinlin/46406c39b4dea903a122 to your computer and use it in GitHub Desktop.
数字转大写
function toChineseNumerals(num) {
if (!/^(\d+)(?:\.(\d{1,2}))?$/.test(num)) return "";
var m = RegExp["$1"], n = RegExp["$2"];
var a = ["", "萬", "億", "兆", "京", "垓", "秭", "穰", "沟", "涧", "正", "载"];
var b = ["", "十", "百", "千"];
var c = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
var d = ["角", "分"];
return "(大写)" + m.split("").reverse().join("").match(/.{1,4}/g).map(function (item, index, array) {
return b.reduceRight(function (prev, cur, idx, arr) {
return prev += c[item[idx] >>> 0] + cur;
}, "").replace(/(零.)+/g, "零").replace(/零+$/, "") + a[index];
}).reverse().join("").replace(/^零+/, "") + "元" + (n ? n.split("").map(function (item, index, array) {
return +item ? (c[+item] + d[index]) : "";
}).join("") : "整");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment