1

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));
Jithesh Kt
  • 2,023
  • 6
  • 32
  • 48

2 Answers2

2

Use maximumSignificantDigits maximumFractionDigits. 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));
KooiInc
  • 119,216
  • 31
  • 141
  • 177
0

There is no build-in solution drop only zero fraction, but you can .replace(/\.0+$/,''); - it will not works for currency format, sorry.

To remove any fraction you can use maximumFractionDigits option of Intl.NumberFormat() constructor.

Anton
  • 728
  • 3
  • 8