Benjoe
Benjoe

Reputation: 466

Removing commas in the standard return value of toLocaleString

I've searched the web and I only found how to add commas between number, but I want to do a reverse which is removing the commas in number.

Here is my JavaScript Code

const format = (num, decimals) => num.toLocaleString('en-US', {
    minimumFractionDigits: 2,      
    maximumFractionDigits: 2,
});

console.log(format(3460));

It returns, 3,460.00. I want it to return 3460.00 only.

Upvotes: 2

Views: 774

Answers (3)

Ritesh Khandekar
Ritesh Khandekar

Reputation: 4005

const format = (num, decimals) => num.toLocaleString('en-US', {
minimumFractionDigits: 2,      
maximumFractionDigits: 2,
}).split(",").join("");

console.log(format(3460));

Upvotes: 1

Abhishek Salgaonkar
Abhishek Salgaonkar

Reputation: 146

According to MDN : The toLocaleString() method returns a string with a language-sensitive representation of this number.

I would do following:

 const parseFloatToFixedPlaces =  (x,decimalPlaces) => {
  if(decimalPlaces === 'undefined')
    {
       decimalPlaces = 0
    }
  return Number.parseFloat(x).toFixed(decimalPlaces);
}

Upvotes: 0

Mark
Mark

Reputation: 92440

If you need toLocaleString, it has a useGrouping option you can set to false:

const format = (num, decimals) => num.toLocaleString('en-US', {
    minimumFractionDigits: 2,      
    maximumFractionDigits: 2,
    useGrouping: false
});

console.log(format(3460));

Upvotes: 3

Related Questions