If you want to use built-in code, you can use toLocaleString() with minimumFractionDigits.

Browser compatibility for the extended options on toLocaleString() was limited when I first wrote this answer, but the current status looks good. If you're using Node.js, you will need to npm install the intl package.

var value = (100000).toLocaleString(
  undefined, // leave undefined to use the visitor's browser 
             // locale or a string like 'en-US' to override it.
  { minimumFractionDigits: 2 }
);
console.log(value);

Number formatting varies between cultures. Unless you're doing string comparison on the output,1 the polite thing to do is pick undefined and let the visitor's browser use the formatting they're most familiar with.2

// Demonstrate selected international locales
var locales = [
  undefined,  // Your own browser
  'en-US',    // United States
  'de-DE',    // Germany
  'ru-RU',    // Russia
  'hi-IN',    // India
  'de-CH',    // Switzerland
];
var n = 100000;
var opts = { minimumFractionDigits: 2 };
for (var i = 0; i < locales.length; i++) {
  console.log(locales[i], n.toLocaleString(locales[i], opts));
}

If you are from a culture with a different format from those above, please edit this post and add your locale code.


1 Which you shouldn't.
2 Obviously do not use this for currency with something like {style: 'currency', currency: 'JPY'} unless you have converted to the local exchange rate. You don't want your website to tell people the price is ¥300 when it's really $300. Sometimes real e-commerce sites make this mistake.

Answer from Michael come lately on Stack Overflow
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Numbers_and_strings
Numbers and strings - JavaScript - MDN Web Docs - Mozilla
We will introduce their underlying representations, and functions used to work with and perform calculations on them. In JavaScript, numbers are implemented in double-precision 64-bit binary format IEEE 754 (i.e., a number between ±2^−1022 and ±2^+1023, or about ±10^−308 to ±10^+308, ...
Top answer
1 of 16
325

If you want to use built-in code, you can use toLocaleString() with minimumFractionDigits.

Browser compatibility for the extended options on toLocaleString() was limited when I first wrote this answer, but the current status looks good. If you're using Node.js, you will need to npm install the intl package.

var value = (100000).toLocaleString(
  undefined, // leave undefined to use the visitor's browser 
             // locale or a string like 'en-US' to override it.
  { minimumFractionDigits: 2 }
);
console.log(value);

Number formatting varies between cultures. Unless you're doing string comparison on the output,1 the polite thing to do is pick undefined and let the visitor's browser use the formatting they're most familiar with.2

// Demonstrate selected international locales
var locales = [
  undefined,  // Your own browser
  'en-US',    // United States
  'de-DE',    // Germany
  'ru-RU',    // Russia
  'hi-IN',    // India
  'de-CH',    // Switzerland
];
var n = 100000;
var opts = { minimumFractionDigits: 2 };
for (var i = 0; i < locales.length; i++) {
  console.log(locales[i], n.toLocaleString(locales[i], opts));
}

If you are from a culture with a different format from those above, please edit this post and add your locale code.


1 Which you shouldn't.
2 Obviously do not use this for currency with something like {style: 'currency', currency: 'JPY'} unless you have converted to the local exchange rate. You don't want your website to tell people the price is ¥300 when it's really $300. Sometimes real e-commerce sites make this mistake.

2 of 16
74

Use

num = num.toFixed(2);

Where 2 is the number of decimal places

Edit:

Here's the function to format number as you want

function formatNumber(number)
{
    number = number.toFixed(2) + '';
    x = number.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '2');
    }
    return x1 + x2;
}

Sorce: www.mredkj.com

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Intl › NumberFormat
Intl.NumberFormat - JavaScript - MDN Web Docs
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: ... const 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.
🌐
W3Schools
w3schools.com › js › js_number_methods.asp
JavaScript Number Methods
The number of milliseconds between 1970-01-02 and 1970-01-01 is 86400000: ... parseInt() parses a string and returns a whole number. Spaces are allowed.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › number › number formatting
Formatting numeric values in JavaScript - 30 seconds of code
February 14, 2024 - We can use String.prototype.padStart() to pad the number to the specified length, after converting it to a string. const padNumber = (n, l) => `${n}`.padStart(l, '0'); padNumber(1_234, 6); // '001234' ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Intl › NumberFormat › format
Intl.NumberFormat.prototype.format() - JavaScript | MDN
July 10, 2025 - A string representing the given number formatted according to the locale and formatting options of this Intl.NumberFormat object.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toLocaleString
Number.prototype.toLocaleString() - JavaScript | MDN
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: ... const number = 123456.789; // German uses comma as decimal separator and period for thousands console.log(number.toLocaleString("de-DE")); // 123.456,789 // Arabic in most Arabic speaking countries uses Eastern Arabic digits console.log(number.toLocaleString("ar-EG")); // ١٢٣٤٥٦٫٧٨٩ // India uses thousands/lakh/crore separators console.log(number.toLocaleString("en-IN")); // 1,23,456.789 // the nu extension key requests a numbering system, e.g.
🌐
Numeraljs
numeraljs.com
Numeral.js
var number = numeral(1000); var string = number.format('0,0'); // '1,000' var value = number.value(); // 1000
Find elsewhere
🌐
Medium
medium.com › @onlinemsr › javascript-string-format-the-best-3-ways-to-do-it-c6a12b4b94ed
JavaScript String Format: The Best 3 Ways To Do It | by Raja MSR | Medium
January 14, 2025 - In this post, we’ve explored different JavaScript string format approaches. Using a plus sign is the traditional approach to formatting the string. You can use the custom function or string prototype method to format the string.
🌐
SimpleLocalize
simplelocalize.io › blog › posts › number-formatting-in-javascript
Number formatting in JavaScript | SimpleLocalize
April 12, 2022 - Not in every language, percentage sign comes after a number. For example, in Arabic languages. const value = 0.767; value.toLocaleString('pl-PL', { style: 'percent' }); // output: 77% value.toLocaleString('ar-SA', { style: 'percent' }); // output:؜ ٧٣٪ ؜ · Units style is one of the most understated JavaScript locale features. It allows you to format number into any popular units with proper formatting for given locale.
🌐
Jonkuperman
jonkuperman.com › javascript-format-number
JavaScript format number
let number = 1000000000000 let result = String(number).replace(/(.)(?=(\d{3})+$)/g, "$1.") This returns '1.000.000.000.000'. A better approach would be to use the built-in toLocaleString which returns language sensitive representations of numbers. We can simply: let number = 1000000000000 let result = number.toLocaleString() This will print '1.000.000.000.000'. What's cool about this approach is, if you are using JavaScript to format money, you can pass in the user's country code and it will adapt.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Text_formatting
Text formatting - JavaScript - MDN Web Docs - Mozilla
November 15, 2024 - This chapter introduces the two most fundamental data types in JavaScript: numbers and strings. We will introduce their underlying representations, and functions used to work with and perform calculations on them. In JavaScript, numbers are implemented in double-precision 64-bit binary format IEEE ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toString
Number.prototype.toString() - JavaScript - MDN Web Docs
Both 0 and -0 have "0" as their string representation. Infinity returns "Infinity" and NaN returns "NaN". If the number is not a whole number, the decimal point . is used to separate the decimal places. Scientific notation is used if the radix is 10 and the number's magnitude (ignoring sign) is greater than or equal to 1021 or less than 10-6.
🌐
Medium
medium.com › @stheodorejohn › mastering-number-formatting-in-javascript-d72acc0453df
Mastering Number Formatting in JavaScript | by Theodore John.S | Medium
October 18, 2023 - In this article, we’ll explore these functions, discussing their purposes, real-world applications, and how they differ from each other. ... Purpose: toFixed() is used to format a number with a fixed number of decimal places. It rounds or ...
🌐
W3Schools
w3schools.com › jsref › jsref_tostring_number.asp
JavaScript Number toString() Method
The toString() returns a number as a string. Every JavaScript object has a toString() method. The toString() method is used by JavaScript when an object needs to be displayed as a text (like in HTML), or when an object needs to be used as a ...
Top answer
1 of 16
1662

Current JavaScript

From ES6 on you could use template strings:

let soMany = 10;
console.log(`This is ${soMany} times easier!`);
// "This is 10 times easier!"

See Kim's answer below for details.


Older answer

Try sprintf() for JavaScript.


If you really want to do a simple format method on your own, don’t do the replacements successively but do them simultaneously.

Because most of the other proposals that are mentioned fail when a replace string of previous replacement does also contain a format sequence like this:

"{0}{1}".format("{1}", "{0}")

Normally you would expect the output to be {1}{0} but the actual output is {1}{1}. So do a simultaneous replacement instead like in fearphage’s suggestion.

2 of 16
1512

Building on the previously suggested solutions:

// First, checks if it isn't implemented yet.
if (!String.prototype.format) {
  String.prototype.format = function() {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function(match, number) { 
      return typeof args[number] != 'undefined'
        ? args[number]
        : match
      ;
    });
  };
}

"{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET")

outputs

ASP is dead, but ASP.NET is alive! ASP {2}


If you prefer not to modify String's prototype:

if (!String.format) {
  String.format = function(format) {
    var args = Array.prototype.slice.call(arguments, 1);
    return format.replace(/{(\d+)}/g, function(match, number) { 
      return typeof args[number] != 'undefined'
        ? args[number] 
        : match
      ;
    });
  };
}

Gives you the much more familiar:

String.format('{0} is dead, but {1} is alive! {0} {2}', 'ASP', 'ASP.NET');

with the same result:

ASP is dead, but ASP.NET is alive! ASP {2}

🌐
Vultr Docs
docs.vultr.com › javascript › examples › format-numbers-as-currency-strings
JavaScript Program to Format Numbers as Currency Strings | Vultr Docs
December 20, 2024 - This JavaScript code listens for changes in an input field (priceInput), formats the entered value as a US Dollar string, and immediately displays the formatted value in another HTML element (formattedPrice). Allow users to select their preferred currency. Format numbers based on the selected currency dynamically.
🌐
BitDegree
bitdegree.org › learn › javascript-number-format
JavaScript Number Format Guide: JavaScript parseFloat Explained
August 8, 2017 - Spaces may be present in the string, but only the first number will be returned. ... Note: if there is no JavaScript integer that can be converted, NaN (Not a Number) is returned.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-formatting
JavaScript String Formatting - GeeksforGeeks
August 5, 2025 - If you insert an arithmetic expression into a string without parentheses, JavaScript may treat the numbers as strings and concatenate them instead of performing math. This can lead to unexpected results. ... JavaScript, unlike languages like C# or Python, doesn’t support built-in string formatting using {} placeholders.