UNPKG

num-beauty

Version:

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

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