accounting-js
Version:
Number, money and currency formatting library.
2 lines • 1.7 kB
JavaScript
var i={symbol:"$",format:"%s%v",decimal:".",thousand:",",precision:2,grouping:3,stripZeros:!1,fallback:0,round:0};function c(t,r=i.decimal,e=i.fallback){let n=new RegExp(`[^0-9-(-)-${r}]`,"g"),o=(""+t).replace(n,"").replace(r,".").replace(/\(([-]*\d*[^)]?\d+)\)/g,"-$1").replace(/\((.*)\)/,""),s=(o.match(/-/g)||"").length%2,f=parseFloat(o.replace(/-/g,""))*(s?-1:1);return isNaN(f)?e:f}function u(t,r=i.decimal,e=i.fallback){return Array.isArray(t)?t.map(n=>u(n,r,e)):c(t,r,e)}function a(t,r,e){r??=i.precision,e??=i.round;let n=Math.pow(10,r),o;return e>0?o=Math.ceil:e<0?o=Math.floor:o=Math.round,(o((t+1e-8)*n)/n).toFixed(r)}function g(t,r){let e=t.split(r),n="";if(e[0]!==void 0&&(n=e[0]),e[1]!==void 0){let o=e[1].replace(/0+$/,"");o.length>0&&(n=n+r+o)}return n}function m(t,r={}){r=Object.assign({},i,r);let e=t<0?"-":"",n=parseInt(a(Math.abs(t),r.precision,r.round),10)+"",o=n.length>3?n.length%3:0,s=e+(o?n.substr(0,o)+r.thousand:"")+n.substr(o).replace(/(\d{3})(?=\d)/g,"$1"+r.thousand)+(r.precision>0?r.decimal+a(Math.abs(t),r.precision).split(".")[1]:"");return r.stripZeros?g(s,r.decimal):s}function p(t,r={}){return Array.isArray(t)?t.map(e=>p(e,r)):m(t,r)}function d(t){return typeof t=="string"&&t.match("%v")?{pos:t,neg:t.replace("-","").replace("%v","-%v"),zero:t}:t}function l(t,r={}){r=Object.assign({},i,r);let e=d(r.format),n;return t>0?n=e.pos:t<0?n=e.neg:n=e.zero,n.replace("%s",r.symbol).replace("%v",m(Math.abs(t),r))}function y(t,r={}){return Array.isArray(t)?t.map(e=>y(e,r)):l(t,r)}export{l as formatMoney,y as formatMoneyArray,m as formatNumber,p as formatNumberArray,i as settings,a as toFixed,c as unformat,u as unformatArray};
//# sourceMappingURL=index.mjs.map