UNPKG

num-beauty

Version:

An ultra lightweight module for formatting numbers into human-friendly strings

1 lines 912 B
import{locales}from"../locales/index.js";export function formatCurrency(r,e,o){const{currency:t,decimals:n=2,showSymbol:m=!0,showCode:i=!1,stripZeros:c=!1}=o,s=locales[e]?.currencies[t];if(!s&&m)throw new Error(`Currency ${t} not supported for locale ${e}`);const u="bigint"==typeof r,a=u?r<0n?-r:r:Math.abs(r),l=r<0||u&&r<0n;let y,f;if(y=u?new Intl.NumberFormat(e,{useGrouping:!0,maximumFractionDigits:0}).format(a):new Intl.NumberFormat(e,{minimumFractionDigits:c?0:n,maximumFractionDigits:n,useGrouping:!0}).format(a),m||i)if(i)f=`${t} ${y}`;else{if(!s)return u?(f=new Intl.NumberFormat(e,{style:"currency",currency:t,maximumFractionDigits:0}).format(a),new Intl.NumberFormat(e,{style:"currency",currency:t,maximumFractionDigits:0}).format(r)):new Intl.NumberFormat(e,{style:"currency",currency:t}).format(r);f="before"===s.position?s.symbol+("$"===s.symbol?"":" ")+y:y+" "+s.symbol}else f=y;return l?"-"+f:f}