UNPKG

numbo

Version:

Convert number and monetary amount to written text. Also helpful for writing cheques (checks). Supports English. 支援中文

155 lines (145 loc) 5.39 kB
// Generated by CoffeeScript 1.12.7 /* numbo.zhCN, as a plugin for Numbo, convert number to Simplified Chinese. This plugin and Numbo are open source in: https://github.com/Edditoria/numbo under MIT license: https://github.com/Edditoria/numbo/blob/master/LICENSE.md */ (function() { var zhCN; zhCN = function(input, options) { var adjustDecimal, check, main, n1, n10, n10Simple, n1Simple, nLarge, normalize, parseCents, speak9999, speakAmt, speakByDigit, speakInt, speakNum, splitNum; if (options == null) { options = 'default'; } n1 = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; n1Simple = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; n10 = ['', '拾', '佰', '仟']; n10Simple = ['', '十', '百', '千']; nLarge = ['', '万', '亿', '兆', '京', '垓', '秭', '穰', '沟', '涧', '正', '载']; speak9999 = function(str, isSimple) { var eachNum, i, num, output, ref, speakN1, speakN10, times, unit; if (isSimple == null) { isSimple = true; } speakN1 = isSimple ? n1Simple : n1; speakN10 = isSimple ? n10Simple : n10; times = str.length - 1; output = ''; for (num = i = 0, ref = times; 0 <= ref ? i <= ref : i >= ref; num = 0 <= ref ? ++i : --i) { eachNum = +str[num]; unit = eachNum === 0 ? '' : speakN10[times - num]; output += speakN1[eachNum] + unit; } if (output === '零零零零') { output = '零'; } else { output = output.replace(/\零+$/g, ''); } return output; }; speakInt = (function(_this) { return function(str, isSimple) { var i, item, itemNum, len, num, output, speakItem, strArr, times, unit; if (isSimple == null) { isSimple = true; } if (str === '0') { return '零'; } else { strArr = _this.tools.splitInt(str, 4); times = strArr.length - 1; output = ''; for (num = i = 0, len = strArr.length; i < len; num = ++i) { item = strArr[num]; speakItem = speak9999(item, isSimple); if (num === 0) { itemNum = parseInt(item, 10); if (itemNum > 9 && itemNum < 20) { speakItem = speakItem.replace(/^[\一\壹]|\零/g, ''); } } unit = speakItem === '零' ? '' : nLarge[times - num]; output += speakItem + unit; } return output.replace(/\零+$/g, '').replace(/\零+/g, '零'); } }; })(this); check = this.tools.check; normalize = this.tools.normalize; splitNum = this.tools.splitNum; parseCents = this.tools.parseCents; adjustDecimal = this.tools.adjustDecimal; speakByDigit = this.tools.speakByDigit; speakNum = function(str) { var dec, dot, int, strSplited; strSplited = splitNum(str); int = strSplited[0]; dec = strSplited[1]; dot = dec === '' ? '' : '点'; return speakInt(int, true) + dot + speakByDigit(dec, n1Simple, ''); }; speakAmt = function(str, options) { var cent, cent1, cent2, dec, dollar, dp1, dp2, int, isSimple, speakN1, strSplited; if (options == null) { options = 'cheque'; } str = adjustDecimal(str, 'ceil', 2); strSplited = splitNum(str); int = strSplited[0]; dec = +((strSplited[1] + '0').slice(0, 2)); if (int === '0' && dec === 0) { return '零元'; } else { isSimple = options === 'cheque' ? false : true; dollar = int === '0' ? '' : int === '2' && options !== 'cheque' ? '两元' : speakInt(int, isSimple) + '元'; cent = dec === 0 ? options === 'cheque' ? '整' : '' : (speakN1 = isSimple ? n1Simple : n1, dp1 = Math.floor(dec / 10), dp2 = dec % 10, cent1 = dp1 === 0 ? '' : dp1 === 2 && options !== 'cheque' ? '两角' : speakN1[dp1] + '角', cent2 = dp2 === 0 ? '' : dp2 === 2 && options !== 'cheque' ? '两分' : speakN1[dp2] + '分', cent = cent1 + cent2); return dollar + cent; } }; main = function(input, options) { if (options == null) { options = 'default'; } if (input === '') { return null; } else if (input === '1e+100') { return 'Ding! One Google... Oops... One Googol!!'; } else { if (check(input) === false) { console.log('Error: Invalid input value. Return null'); return null; } else { input = normalize(input); input; switch (options) { case 'default': case 'number': case 'num': return speakNum(input); case 'cheque': case 'check': case 'chk': case 'chq': return speakAmt(input, 'cheque'); case 'amount': case 'amt': return speakAmt(input, 'amount'); default: console.log('Error: Option in zhCN is not valid'); return null; } } } }; return main(input, options); }; if ((typeof module !== "undefined" && module !== null) && module.exports) { module.exports = zhCN; } if (typeof window !== "undefined" && window !== null) { window.numbo.zhCN = zhCN; } }).call(this);