Navigation

Javascripts

Code snippets for Manipulating Objects, Arrays, Time, Currency in Javascript

Convert Number to Currency Format

1export const convertToCurrency = (
2    num: number,
3    {
4        locale = "en-US",
5        currency = "THB",
6        addPrefix = false,
7        decimal = 2,
8        includeCent = true
9    }
10) => {
11    const formatter = new Intl.NumberFormat(locale, {
12        style: "currency",
13        currency,
14        minimumFractionDigits: decimal
15    });
16    num = includeCent ? num / 100 : num;
17    const formatted = formatter.format(num);
18    return addPrefix ? formatted : formatted.slice(currency.length + 1);
19};