174

How can I do this in JavaScript?

var num = 2046430; 
num.toLocaleString();

will give you "2,046,430";

What I have tried is:

var num = 2046430; 
num.toLocaleString().toFixed(2);

Expected Output

"2,046,430.00"

1
  • toFixed() is only for number not strings Commented Jul 19, 2023 at 16:07

3 Answers 3

392

Taken from MDN:

Syntax

numObj.toLocaleString([locales [, options]])

toLocaleString takes 2 arguments. The first is the locale, the second are the options. As for the options, you are looking for:

minimumFractionDigits

The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information).

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

To be able to set the options without setting the locale, you can pass undefined as first argument:

var num = 2046430;
num.toLocaleString(undefined, {minimumFractionDigits: 2}) // 2,046,430.00

However this also allows the fraction to be longer than 2 digits. So we need to look for one more option called maximumFractionDigits. (Also on that MDN page)

var num = 2046430.123;


console.log(num.toLocaleString(undefined, {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
})); // 2,046,430.12

Sign up to request clarification or add additional context in comments.

4 Comments

Also one more thing. You can do things like this for convenience reasons: jsfiddle.net/st31wm96/2 becaue it could be quite repetitive to always write this by hand.
this is not same as toFixed(2).. Example 33.6.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }) will be equal "33.6" not "33.60"
@Zalaboza, I just tested in the console and your example code does indeed show "33.60". The accepted answer works as expected.
toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
1

I came here because I would like to show the currency symbol (R$) in addition show two digits after the decimal point in the result.

Initially I was trying the following code:

`Amount ${(gas * litros).toFixed(2)
  .toLocaleString('pt-BR', {style: 'currency', currency: 'BRL'})}`

Expected output: Total a pagar R$ 20.95

Output: Total a pagar 20.95

So, with the answers above, I tried without the toFixed():

`Total a pagar ${(gas * litros)
    .toLocaleString('pt-BR', {style: 'currency', currency: 'BRL', minimumFractionDigits: 2})}`

Output: Total a pagar R$ 15,80

Comments

0

@Sebastian Nette's accepted answer was, for some reason not working for me to handle numbers like "1,230.05", where it was a string with a comma.

I ended up going with the following:

var num = "1,230.05";
parseFloat(num.replace(",", "")).toFixed(2);

It just manually strips the commas before parsing as a float and running toFixed().

2 Comments

It seems like you're solving a different problem. Sebastian starts with a number and formats it as a locale string with 2 digits after the decimal. Your answer starts with a locale string and formats it as a non-localized number with 2 digits after the decimal.
Removing , is not locale aware while the question explicitly asks about toLocaleString. Some locales use , instead of ., eg 1 230,05 (space instead of commas for thousands)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.