Javascript: using toLocaleString + Tofixed

You can give an object to .toLocaleString() which describes what you want:

var sNumber = (10123.322).toLocaleString(undefined,
 {'minimumFractionDigits':2,'maximumFractionDigits':2});

Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

Original:

const fNumber = 10123.322;
const sNumber = parseFloat(fNumber.toFixed(2)).toLocaleString();
console.log(sNumber);

The number is already in decimal/float format on the first line.

  • .toFixed(2) turns it into a string using fixed-point notation.
  • parseFloat() takes that string and turns it back into a float.
  • .toLocaleString() turns it into a string using the local format.

Just to do it in one line

var num = '12233.3366554';
num = parseFloat(parseFloat(num).toFixed(2)).toLocaleString('en-IN', { useGrouping: true });