使用 JavaScript 从字符串中删除逗号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5788741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:51:45  来源:igfitidea点击:

Remove commas from the string using JavaScript

javascript

提问by Kanak Vaghela

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

我想从字符串中删除逗号并使用 JavaScript 计算这些数量。

For example, I have those two values:

例如,我有这两个值:

  • 100,000.00
  • 500,000.00
  • 100,000.00
  • 500,000.00

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

现在我想从这些字符串中删除逗号并想要这些数量的总和。

回答by lonesomeday

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

要删除逗号,您需要replace在字符串上使用。要转换为浮点数以便进行数学运算,您需要parseFloat

var total = parseFloat('100,000.00'.replace(/,/g, '')) +
            parseFloat('500,000.00'.replace(/,/g, ''));

回答by Vincent Tang

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('34.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 NaNyou 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.

通过 使用本地国际日期format。这会清除任何错误的输入,如果有,它会返回一串NaN您可以检查的输入。目前无法将逗号作为区域设置的一部分(截至 10 年 12 月 19 日)删除,因此您可以使用正则表达式命令删除逗号replace

ParseFloatconverts the this type definition from string to number

ParseFloat将 this 类型定义从字符串转换为数字

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

如果你使用 React,这就是你的计算函数的样子:

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
}