|
1
|
export const getLocaleAmount = (amount: number, currencyCode: string) => { |
|
2
|
const formatter = new Intl.NumberFormat([], { |
|
3
|
style: "currency", |
|
4
|
currencyDisplay: "narrowSymbol", |
|
5
|
currency: currencyCode, |
|
6
|
}) |
|
7
|
|
|
8
|
return formatter.format(amount) |
|
9
|
} |
|
10
|
|
|
11
|
export const getNativeSymbol = (currencyCode: string) => { |
|
12
|
const formatted = new Intl.NumberFormat([], { |
|
13
|
style: "currency", |
|
14
|
currency: currencyCode, |
|
15
|
currencyDisplay: "narrowSymbol", |
|
16
|
}).format(0) |
|
17
|
|
|
18
|
return formatted.replace(/\d/g, "").replace(/[.,]/g, "").trim() |
|
19
|
} |
|
20
|
|
|
21
|
export const getDecimalDigits = (currencyCode: string) => { |
|
22
|
const formatter = new Intl.NumberFormat(undefined, { |
|
23
|
style: "currency", |
|
24
|
currency: currencyCode, |
|
25
|
}) |
|
26
|
|
|
27
|
return formatter.resolvedOptions().maximumFractionDigits |
|
28
|
} |
|
29
|
|
|
30
|
export const getStylizedAmount = (amount: number, currencyCode: string) => { |
|
31
|
const symbol = getNativeSymbol(currencyCode) |
|
32
|
const decimalDigits = getDecimalDigits(currencyCode) |
|
33
|
|
|
34
|
const total = amount.toLocaleString(undefined, { |
|
35
|
minimumFractionDigits: decimalDigits, |
|
36
|
maximumFractionDigits: decimalDigits, |
|
37
|
}) |
|
38
|
|
|
39
|
return `${symbol} ${total} ${currencyCode.toUpperCase()}` |
|
40
|
} |