130

I want to remove commas from the string and calculate those amount using JavaScript.

For example, I have those two values:

  • 100,000.00
  • 500,000.00

Now I want to remove commas from those string and want the total of those amount.

1
  • 1
    It's not efficient, but you could always do "1,000,000.00".split(',").join(""). Commented Mar 23, 2021 at 19:39

3 Answers 3

238

To remove the commas, you'll need to use replace on the string. To convert to a float so you can do the maths, you'll need parseFloat:

var total = parseFloat('100,000.00'.replace(/,/g, '')) +
            parseFloat('500,000.00'.replace(/,/g, ''));
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, need to combine replace and parseFloat. here is quick test case: jsfiddle.net/TtYpH
It's 2017, is there no way to go to and from a locale string? How do you reverse this function? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Posted a separate question here: stackoverflow.com/questions/41905406/…
7

Related answer, but if you want to run clean up a user inputting values into a form, here's what you can do:

const numFormatter = new Intl.NumberFormat('en-US', {
  style: "decimal",
  maximumFractionDigits: 2
})

// Good Inputs
parseFloat(numFormatter.format('1234').replace(/,/g,"")) // 1234
parseFloat(numFormatter.format('123').replace(/,/g,"")) // 123

// 3rd decimal place rounds to nearest
parseFloat(numFormatter.format('1234.233').replace(/,/g,"")); // 1234.23
parseFloat(numFormatter.format('1234.239').replace(/,/g,"")); // 1234.24

// Bad Inputs
parseFloat(numFormatter.format('1234.233a').replace(/,/g,"")); // NaN
parseFloat(numFormatter.format('$1234.23').replace(/,/g,"")); // NaN

// Edge Cases
parseFloat(numFormatter.format(true).replace(/,/g,"")) // 1
parseFloat(numFormatter.format(false).replace(/,/g,"")) // 0
parseFloat(numFormatter.format(NaN).replace(/,/g,"")) // NaN

Use the international date local via format. This cleans up any bad inputs, if there is one it returns a string of NaN you can check for. There's no way currently of removing commas as part of the locale (as of 10/12/19), so you can use a regex command to remove commas using replace.

ParseFloat converts the this type definition from string to number

If you use React, this is what your calculate function could look like:

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}

Comments

5

To remove commas, you will need to use string replace method.

var numberArray = ["1000,00", "23", "11"];

//If String
var arrayValue = parseFloat(numberArray.toString().replace(/,/g, ""));

console.log(arrayValue, "Array into toString")

// If Array

var number = "23,949,333";
var stringValue = parseFloat(number.replace(/,/g, ""));

console.log(stringValue, "using String");

Comments

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.