UNPKG

ll-package

Version:

2 lines (1 loc) 1.89 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function g(e){if(!e&&e!==0)return"";const t=+e;if(Number.isNaN(t)||t<0)return"";const r=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"],c=["","拾","佰","仟"],l=["","万","亿","兆"],o=["角","分","厘","毫"],[n,f]=t.toString().split(".");if(n&&n.length>15)return"数字较大溢出";let u="";if(n)for(let i=0,s=n.length;i<s;i++){const a=n.charAt(i),h=s-i-1;if(a==="0"){if(i===s-1){n.length===1&&(u+="零");break}if(n.charAt(i+1)==="0")continue}u+=r[a],parseInt(a)&&(u+=c[h%4]),h%4===0&&(u+=l[Math.floor(h/4)])}if(u+="元",parseInt(f))for(let i=0;i<4;i++){const s=f.charAt(i);parseInt(s)&&(u+=r[s]+o[i])}else u+="整";return u}function N(e){if(e===0)return"0";if(!e)return"";const t=e.toString().split(".");return t[0]=t[0].replace(/\B(?=(\d{3})+(?!\d))/g,","),t.join(".")}const m=e=>{const t=[],r=c=>/^[\d.]$/.test(c);for(let c=0;c<e.length;c++){if(c>0&&r(e[c-1])&&r(e[c])){t[t.length-1]+=e[c];continue}t.push(e[c])}return t},d=e=>{const t=[],r=[],c=o=>o.length>0?o[o.length-1]:null,l=(o,n)=>{const f=u=>["+-","×÷","()"].findIndex(i=>i.includes(u));return f(o)-f(n)};for(e.forEach(o=>{if(typeof o=="string"&&Number.isNaN(Number(o))){if(o==="(")t.push(o);else if(o===")"){let n;do n=t.pop(),n!=="("&&r.push(n);while(t.length&&n!=="(")}else{let n=c(t);if(!n)t.push(o);else{for(;n&&n!=="("&&l(n,o)>=0;)r.push(t.pop()),n=c(t);t.push(o)}}return}r.push(o)});t.length;)r.push(t.pop());return r},p=e=>{e=e.concat();const t=(r,c,l)=>{const o=Number(r),n=Number(c);switch(l){case"+":return o+n;case"-":return o-n;case"×":return o*n;case"÷":return o/n}};for(let r=2;r<e.length;r++)if("+-×÷".includes(e[r])){const c=t(e[r-2],e[r-1],e[r]);e.splice(r-2,3,c),r=r-2}return e[0]};exports.calcRPN=p;exports.getAmountChinese=g;exports.mergeNumberOfExps=m;exports.thousandsFormat=N;exports.toRPN=d;