I'm working on a banking app front-end written in TypeScript.
From the back-end, I receive:
- an amount with 2 decimal places (e.g. account balance) in string. This can potentially be a large value - over 15 digits before the decimal point.
- this is formatted as:
"32012012012012312.09"
- this is formatted as:
- a currency code string (e.g.
"USD"
)
I'd like to format the amount with thousand separators, a currency symbol, and NBSP/NNBSP handling, all with respect to provided locale. What's the easiest way?
Are there potential solutions to partial problems? E.g. 1. formatting the number and 2. formatting with currency symbol.
What I tried:
Intl.NumberFormat
- I like it, but it takes anumber | bigint
(although it doesn't crash with strings), but it stops being precise at large decimal numbers - e.g. it formats32_012_012_012_012_312.09
as32,012,012,012,012,310.00
- I believe this is a JavaScript limitation regarding numbers, that's why I'm looking for something handling stringsaccounting-js
-formatMoney
function with both string and number - same result and missing functionality of passing the locale in- looking for other libraries and threads on this topic - I looked a bit into big/bignumber/decimal.js and numeral.js, haven't tried them, but I believe they are about handling big numbers, not formatting or even locale-based formatting