Q:

How to add comma in a number in string

So I know how to add a comma on numbers (toLocaleString.()) and this function requires integer or decimal value as a parameter. I need this result with 2 digit decimal value. It does run and return me correct data but I want decimal to be fixed to 2 digits like below

var num = 66666.7
console.log("original", num)
console.log("with comma", num.toLocaleString())
console.log("with 2 digit fixed", num.toFixed(2))
console.log("not working--", (num.toFixed(2)).toLocaleString())
console.log("error--", (num.toLocaleString()).toFixed(2))



It doesn't work with toFixed() values
Suggest anything, please!

A:

You can specify the exact number of decimal digits in your options, which is the second parameter in toLocaleString()
const number = 24242324.5754;

number.toLocaleString('en-US', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
})

// result is: 24,242,324.58

See also MDN doc here

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
maximumFractionDigits
The maximum number of fraction digits to use.
  Possible values are from 0 to 20; the default for plain number
  formatting is the larger of minimumFractionDigits and 3

