What is the best way to remove the trailing zeros from the output when using Javascript NumberFormat to display Swedish currency?
I want output 200 kr instead 200,00 kr
Is there a built-in solution with Intl.NumberFormat to achieve this?
console.log(Intl.NumberFormat('sv-SE', {style:'currency', currency: 'SEK'}).format(200));
Use maximumSignificantDigitsmaximumFractionDigits. Something like:
function getAmount(number) {
if ((number | 0) < number) {
return Intl.NumberFormat('sv-SE', {
style: 'currency',
currency: 'SEK'
}).format(number)
}
return Intl.NumberFormat('sv-SE', {
style: 'currency',
currency: 'SEK',
maximumFractionDigits: 0
}).format(number);
}
console.log(getAmount(200));
console.log(getAmount(200.23));
There is no build-in solution drop only zero fraction, but you can - it will not works for currency format, sorry..replace(/\.0+$/,'');
To remove any fraction you can use maximumFractionDigits option of Intl.NumberFormat() constructor.