UNPKG

@happys/money-format

Version:
1 lines 5.51 kB
var moneyFormat=function(r){var e={};function t(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return r[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=r,t.c=e,t.d=function(r,e,n){t.o(r,e)||Object.defineProperty(r,e,{enumerable:!0,get:n})},t.r=function(r){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})},t.t=function(r,e){if(1&e&&(r=t(r)),8&e)return r;if(4&e&&"object"==typeof r&&r&&r.__esModule)return r;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:r}),2&e&&"string"!=typeof r)for(var o in r)t.d(n,o,function(e){return r[e]}.bind(null,o));return n},t.n=function(r){var e=r&&r.__esModule?function(){return r.default}:function(){return r};return t.d(e,"a",e),e},t.o=function(r,e){return Object.prototype.hasOwnProperty.call(r,e)},t.p="",t(t.s=1)}([function(r,e,t){"use strict";var n=this&&this.__assign||function(){return(n=Object.assign||function(r){for(var e,t=1,n=arguments.length;t<n;t++)for(var o in e=arguments[t])Object.prototype.hasOwnProperty.call(e,o)&&(r[o]=e[o]);return r}).apply(this,arguments)};Object.defineProperty(e,"__esModule",{value:!0}),e.sum=e.initOptions=void 0,e.initOptions={decimals:2,round:!0},e.sum=function(r,t){var o=n(n({},e.initOptions),t),i=o.decimals,a=o.round,u=function(r){return r.map((function(r){var e=Number(r);return isNaN(e)?null:e})).filter((function(r){return null!==r}))}(r);if(0===u.length)return"0";try{var f=u.reduce((function(r,e){return r+e}),0);if(Math.floor(i)!==i||i<0||i>14)throw new Error("Invalid decimals value");var l=Math.ceil(f*Math.pow(10,i+1)),c=Math.floor(l/10);return a&&(c=Math.round(l/10)),(c/Math.pow(10,i)).toFixed(i)}catch(r){return console.error("Error in sum calculation:",r),"0"}}},function(r,e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.difference=e.sum=e.RMB=e.format=void 0;var n=t(2);Object.defineProperty(e,"format",{enumerable:!0,get:function(){return n.format}});var o=t(3);Object.defineProperty(e,"RMB",{enumerable:!0,get:function(){return o.RMB}});var i=t(0);Object.defineProperty(e,"sum",{enumerable:!0,get:function(){return i.sum}});var a=t(4);Object.defineProperty(e,"difference",{enumerable:!0,get:function(){return a.difference}})},function(r,e,t){"use strict";var n=this&&this.__assign||function(){return(n=Object.assign||function(r){for(var e,t=1,n=arguments.length;t<n;t++)for(var o in e=arguments[t])Object.prototype.hasOwnProperty.call(e,o)&&(r[o]=e[o]);return r}).apply(this,arguments)};Object.defineProperty(e,"__esModule",{value:!0}),e.format=void 0;var o={decimal:2,delimiter:",",digit:3,round:!0};e.format=function(r,e){void 0===e&&(e=o);var t=n(n({},o),e),i=t.decimal,a=t.delimiter,u=t.digit,f=t.round,l=Math.ceil(Number(r)*Math.pow(10,i+1)),c=Math.floor(l/10);f&&(c=Math.round(l/10));var s=(c/Math.pow(10,i)).toFixed(i),p=s.split(".")[0],d=s.split(".")[1],v=new RegExp("(\\d)(?=(\\d{"+u+"})+$)","gi"),h=p.replace(v,"$1"+a);return d||void 0!==d?h+"."+d:h}},function(r,e,t){"use strict";var n=this&&this.__assign||function(){return(n=Object.assign||function(r){for(var e,t=1,n=arguments.length;t<n;t++)for(var o in e=arguments[t])Object.prototype.hasOwnProperty.call(e,o)&&(r[o]=e[o]);return r}).apply(this,arguments)};Object.defineProperty(e,"__esModule",{value:!0}),e.RMB=void 0;var o=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"],i=["元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万","拾","佰"],a=["角","分"],u={negativeStr:"负",integerStr:"整",prefix:""};e.RMB=function(r,e){void 0===e&&(e=u),function(r){if("number"!=typeof r&&"string"!=typeof r)throw new Error("money type must to number or string");if(Number(r)>999999999999980)throw new Error("money not greater than 999999999999980")}(r);var t=n(n({},u),e),f=t.prefix,l=t.negativeStr,c=t.integerStr,s=Number(r),p=s<0?l:"",d=function(r,e){for(var t="",n=0;n<a.length;n++)t+=(o[Math.floor(10*r*Math.pow(10,n))%10]+a[n]).replace(/零./,"");return t||e}(s=Math.abs(s),c);return f+p+function(r){for(var e=Math.floor(r),t=[],n=0;n<i.length&&e>0;n++){var a=o[Math.floor(e%10)]+i[n];t.push(a),e=Math.floor(e/10)}return t.reverse().join("").replace(/(零)([元万亿])/g,"$2$1").replace(/(零[拾佰仟])+/g,"零").replace(/零+/g,"零").replace(/(零(?=[元万亿]))*/g,"").replace(/(亿万)/g,"亿").replace(/零$/,"")||"零元"}(s)+d}},function(r,e,t){"use strict";var n=this&&this.__assign||function(){return(n=Object.assign||function(r){for(var e,t=1,n=arguments.length;t<n;t++)for(var o in e=arguments[t])Object.prototype.hasOwnProperty.call(e,o)&&(r[o]=e[o]);return r}).apply(this,arguments)};Object.defineProperty(e,"__esModule",{value:!0}),e.difference=void 0;var o=t(0);function i(r){return"number"==typeof r&&!isNaN(r)}function a(r){if("string"==typeof r){var e=parseFloat(r);if(!isNaN(e))return e}else if("number"==typeof r)return r;throw new Error("Invalid number format")}e.difference=function(r,e,t){var u=n(n({},o.initOptions),t),f=u.decimals,l=u.round;if(!r||!e)throw new Error("val1 or val2 is empty");if(!i(a(r))||!i(a(e)))throw new Error("val1 or val2 is not a valid number");if(Math.floor(f)!==f||f<0||f>14)throw new Error("Invalid decimals value");try{var c=a(r)-a(e),s=Math.ceil(c*Math.pow(10,f+1)),p=Math.floor(s/10);return l&&(p=Math.round(s/10)),(p/Math.pow(10,f)).toFixed(f)}catch(r){throw console.error("Error in difference calculation:",r),new Error("Calculation failed")}}}]);