fireship-io / fireship.io

Build and ship your app faster https://fireship.io
3.55k stars 1.31k forks source link

snippets/currency-formatting/ #713

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Format currency with Intl

Use Intl.NumberFormat() to convert currency, units, notation, and other numeric values

https://fireship.io/snippets/currency-formatting/

NishantJS commented 2 years ago

How to format currency with default locale?

nofurtherinformation commented 2 years ago

@NishantJS you can use undefined as the provided locale:

const number = 123456.789;

const myNum = new Intl.NumberFormat(
    undefined, //locale, use user default
    { 
            notation: "compact",
        compactDisplay: "short" 
        }
).format(number));

console.log(myNum)