Skip to content

intl numberformat no decimals | Example code

  • by

Set minimumFractionDigits = 0 to get intl numberformat no decimals in JavaScript. But it will not add zero decimals. If you want to remove all decimals, then use the toFixed() method.

Example intl numberformat no decimals

Simple example code.

<!DOCTYPE html>
<html>
<body>

  <script>
   const formatter = new Intl.NumberFormat('en-NZ', {
    style: 'currency',
    currency: 'NZD',
    minimumFractionDigits: 0,
  });

   let number = 4.1;
   console.log(formatter.format(number.toFixed()));

   number = 4;
   console.log(formatter.format(number));

 </script>

</body>
</html>

Output:

intl numberformat no decimals

Intl.NumberFormat either 0 or two fraction digits

The correct way to do it when using Intl.NumberFormat is to set both maximum fraction digits and minimumFractionDigits options in the constructor while validating input value using a modulo % for a whole number.

const fraction = new Intl.NumberFormat('en-NZ', {
  style: 'currency',
  currency: 'NZD',
  minimumFractionDigits: 0,
  maximumFractionDigits: 0,
});

const formatter = new Intl.NumberFormat('en-NZ', {
  style: 'currency',
  currency: 'NZD',
  minimumFractionDigits: 2,
});

let number = 4.1;
  if(number % 1 == 0)
console.log(fraction.format(number));
  else
console.log(formatter.format(number));

number = 4;
  if(number % 1 == 0)
console.log(fraction.format(number));
  else
console.log(formatter.format(number));

Or you could try something like this:

formatter.format(amount).replace(/\D00(?=\D*$)/, '');

Do comment if you have any doubts or suggestions on this JS number format topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *