The correct way to do it when using Intl.NumberFormat is to set both maximumFractionDigits and minimumFractionDigits options in constructor while validating input value using a modulo % for a whole number (per https://stackoverflow.com/a/49724586/1362535 which is a CORRECT answer!). The accepted answer is sort of string manipulation.

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));

Answer from crtag on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Intl › NumberFormat › NumberFormat
Intl.NumberFormat() constructor - JavaScript - MDN Web Docs
January 25, 2026 - The integer and fraction digit properties indicate the number of digits to display before and after the decimal point, respectively. If the value to display has fewer integer digits than specified, it will be left-padded with zeros to the expected number. If it has fewer fractional digits, it will be right-padded with zeros. Both cases are shown below: ... // Formatting adds zeros to display minimum integers and fractions console.log( new Intl.NumberFormat("en", { minimumIntegerDigits: 3, minimumFractionDigits: 4, }).format(4.33), ); // "004.3300"
🌐
DEV Community
dev.to › orimdominic › formatting-numbers-in-javascript-with-intl-numberformat-5goo
Formatting Numbers in JavaScript with Intl.NumberFormat - DEV Community
August 24, 2024 - That's because the minimum number of decimal digits we set in the options parameter as formatConfig, (minimumFractionDigits) was 2. See? Apart from the format method on the formatterObject, there is also another method, the formatToParts method ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Intl › NumberFormat
Intl.NumberFormat - JavaScript - MDN Web Docs
Getter function that formats a range of numbers according to the locale and formatting options of the Intl.NumberFormat object from which the method is called.
🌐
V8
v8.dev › features › intl-numberformat
Intl.NumberFormat · V8
August 8, 2019 - const formatter = new Intl.NumberFormat('en'); formatter.format(987654.321); // → '987,654.321' formatter.formatToParts(987654.321); // → [ // → { type: 'integer', value: '987' }, // → { type: 'group', value: ',' }, // → { type: 'integer', value: '654' }, // → { type: 'decimal', value: '.' }, // → { type: 'fraction', value: '321' } // → ] Note: Although much of the Intl.NumberFormat functionality can be achieved using Number.prototype.toLocaleString, Intl.NumberFormat is often the better choice, since it enables creating a re-usable formatter instance which tends to be more efficient.
🌐
Titaniumsdk
titaniumsdk.com › api › global › intl › numberformat.html
Global.Intl.NumberFormat | Titanium SDK
// Set up percentage style numeric ... of 1, it is rounded. console.log(formatter.format(12.3456)); // Logs: 1.235E5 // Decimal point will be a period, comma, or space depending on the locale....
🌐
W3C
w3c.github.io › i18n-drafts › questions › qa-number-format.en.html
Number, currency, and unit formatting
2 weeks ago - The options object in new Intl.NumberFormat(locales, options) is where the magic happens. minimumFractionDigits and maximumFractionDigits control the number of decimal places.
🌐
Pub.dev
pub.dev › documentation › intl › latest › intl › NumberFormat-class.html
NumberFormat class - intl library - Dart API
Creates a NumberFormat for currencies, using the simple symbol for the currency if one is available (e.g. $, €), so it should only be used if the short currency symbol will be unambiguous. ... The name of the currency to print, in ISO 4217 form. ... The symbol to be used when formatting this as currency. ... The number of decimal places to use when formatting.
Find elsewhere
🌐
JavaScript in Plain English
javascript.plainenglish.io › intl-numberformat-api-a-comprehensive-guide-f310a760733b
Intl.NumberFormat API: A Comprehensive Guide | by Danielle Dias | JavaScript in Plain English
June 13, 2023 - Before we delve into the specifics of the Intl.NumberFormat API, let’s establish a basic understanding of what it offers. The API enables developers to format numbers based on the conventions of a specific locale. This includes formatting for things such as decimal separators, grouping ...
🌐
Elisabethirgens
elisabethirgens.github.io › notes › 2020 › 03 › format-numbers
Format Numbers With Intl.NumberFormat
March 26, 2020 - It returns US style thousand separator comma and decimal point, while the Norwegian locale uses space and decimal comma. Intl.NumberFormat("en-US").format(123456.789); // returns "123,456.789" Intl.NumberFormat("nb-NO").format(123456.789); // returns "123 456,789"
🌐
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › Intl › NumberFormat
Intl.NumberFormat - JavaScript
var number = 3500; console.log(new Intl.NumberFormat().format(number)); // → '3,500' if in US English locale · This example shows some of the variations in localized number formats. In order to get the format of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the locales argument: var number = 123456.789; // German uses comma as decimal separator and period for thousands console.log(new Intl.NumberFormat('de-DE').format(number)); // → 123.456,789 // Arabic in most Arabic speaking countries uses real Arabic digits console.log(new Intl.NumberFormat('ar-EG').format(number)); // → ١٢٣٤٥٦٫٧٨٩ // India uses thousands/lakh/crore separators console.log(new Intl.NumberFormat('en-IN').format(number)); // → 1,23,456.789 // the nu extension key requests a numbering system, e.g.
🌐
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › Intl › NumberFormat › NumberFormat
Intl.NumberFormat() constructor - JavaScript
let amount = 3500; new Intl.NumberFormat('en-US', {style: 'decimal'}).format(amount); // → '3,500' new Intl.NumberFormat('en-US', {style: 'percent'}).format(amount); // → '350,000%'
🌐
GitHub
github.com › tc39 › ecma402 › issues › 286
NumberFormat: Hide fraction digits when formatting an integer · Issue #286 · tc39/ecma402
October 21, 2018 - NumberFormat doesn't actually allow you to format numbers in a way that I would say is "human friendly". Let's take her example. If we have a whole number like 12, we want to display that as a simple $12, but if we've got something like 12.3 ...
Author   evilpie
🌐
W3docs
w3docs.com › javascript
How to Format a Number with Two Decimals in JavaScript
const formatter = new Intl.NumberFormat('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2, }); console.log(formatter.format(3.005)); // "3.01" console.log(formatter.format(2.345)); // "2.35" ...
🌐
Codú
codu.co › articles › formatting-money-in-javascript-with-intl-numberformat-u3-zr6-t
Formatting Money in JavaScript with Intl.NumberFormat | by Niall Maher | Codú
You can adjust the number of decimal places shown with maximumFractionDigits to 0, and it just works. ... const usFormatterNoCents = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0, }); ...
🌐
Codeproject
reference.codeproject.com › javascript
javascript Intl.NumberFormat - CodeProject Reference
November 5, 2021 - Chinese decimal console.log(new Intl.NumberFormat('zh-Hans-CN-u-nu-hanidec').format(number)); // → 一二三,四五六.七八九 // when requesting a language that may not be supported, such as // Balinese, include a fallback language, in this case Indonesian console.log(new Intl.NumberFormat(['ban', 'id']).format(number)); // → 123.456,789
🌐
TC39
tc39.es › proposal-intl-numberformat-v3 › out › numberformat › proposed.html
1.1 The Intl.NumberFormat Constructor
Let fraction be the substring of n from position decimalSepIndex, exclusive, to the end of n. ... Let integer be n. ... Let groupSepSymbol be the implementation-, locale-, and numbering system-dependent (ILND) String representing the grouping separator. Let groups be a List whose elements are, in left to right order, the substrings defined by ILND set of locations within the integer, which may depend on the value of numberFormat.[[UseGrouping]].
🌐
DEV Community
dev.to › schalkneethling › number-and-currency-formatting-in-javascript-using-intlnumberformat-46og
Number and Currency Formatting in JavaScript using Intl.NumberFormat - DEV Community
April 3, 2024 - This is not ideal for several reasons. Our decimal value is hard coded as is our currency symbol. It is also hard to read and error-prone. In addition, the hard-coded decimal value is especially problematic. One of the many things NumberFormat can help us with is with currencies.
🌐
GitHub
github.com › tc39 › proposal-unified-intl-numberformat
GitHub - tc39/proposal-unified-intl-numberformat: Adds localized formatting of measurement units, compact decimals, and more number features · GitHub
January 26, 2022 - This proposal adds measurement units, compact decimal notation, and other localized number formatting features to Intl.NumberFormat.
Starred by 49 users
Forked by 9 users
Languages   HTML
🌐
David Walsh
davidwalsh.name › intl-numberformat
Intl.NumberFormat
April 13, 2021 - new Intl.NumberFormat().format(12345) // 12,345 new Intl.NumberFormat('en-US', { maximumSignificantDigits: 4}).format(1.2345678) // 1.235 (Notice the rounding) new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'GBP' }).format(9002.20) // £9,002.20 new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(9002.20) // 9.002,20 € new Intl.NumberFormat().formatToParts(12345.678) /* [ { "type":"integer", "value":"12" }, { "type":"group", "value":"," }, { "type":"integer", "value":"345" }, { "type":"decimal", "value":"." }, { "type":"fraction", "value":"678" } ] */ Don't bother writing your own client side number formatting functions if the numbers you want to present are standard formats -- leverage the amazing APIs the browser provides you!