And*_*rus 43 javascript jquery data-conversion string-parsing
输入元素包含数字a,其中逗号或点用作小数分隔符,空格可用于对数千个进行分组,如下所示:
'
1,2''110 000,23''100
1.23'
如何使用JavaScript将它们转换为浏览器中的浮点数?
使用jQuery和jQuery UI.Number(string)返回NaN并parseFloat()在第一个空格或逗号处停止.
小智 44
首先替换:
parseFloat(str.replace(',','.').replace(' ',''))
Run Code Online (Sandbox Code Playgroud)
Tho*_*4no 17
我意识到我迟到了,但我想要一个解决方案,正确处理数字分组以及货币的不同小数分隔符.由于这些都没有完全覆盖我的用例,我编写了自己的解决方案,这可能对其他人有用:
function parsePotentiallyGroupedFloat(stringValue) {
stringValue = stringValue.trim();
var result = stringValue.replace(/[^0-9]/g, '');
if (/[,\.]\d{2}$/.test(stringValue)) {
result = result.replace(/(\d{2})$/, '.$1');
}
return parseFloat(result);
}
Run Code Online (Sandbox Code Playgroud)
这应该去掉任何非数字,然后检查是否有一个小数点(或逗号)后跟两位数字,并在需要时插入小数点.
值得注意的是,我专门针对货币设定了这种方式,因此它假设没有小数位或正好两位.很难确定遇到的第一个可能的小数点是小数点还是数字分组字符(例如,1.542可能1542),除非你知道当前语言环境的细节,但它应该很容易定制到你的通过更改\d{2}$为与小数点后的预期相匹配的特定用例.
您可以用空字符串替换所有空格,所有逗号都用点替换,然后解析它.
var str = "110 000,23";
var num = parseFloat(str.replace(/\s/g, "").replace(",", "."));
console.log(num);
Run Code Online (Sandbox Code Playgroud)
我在第一个中使用正则表达式来匹配所有空格,而不仅仅是第一个空格.
关于什么:
parseFloat(str.replace(' ', '').replace('.', '').replace(',', '.'));
Run Code Online (Sandbox Code Playgroud)
所有其他解决方案都要求您提前了解格式。我需要检测(!)每种情况下的格式,这就是我最终得到的结果。
function detectFloat(source) {
let float = accounting.unformat(source);
let posComma = source.indexOf(',');
if (posComma > -1) {
let posDot = source.indexOf('.');
if (posDot > -1 && posComma > posDot) {
let germanFloat = accounting.unformat(source, ',');
if (Math.abs(germanFloat) > Math.abs(float)) {
float = germanFloat;
}
} else {
// source = source.replace(/,/g, '.');
float = accounting.unformat(source, ',');
}
}
return float;
}
Run Code Online (Sandbox Code Playgroud)
这是通过以下情况进行测试的:
const cases = {
"0": 0,
"10.12": 10.12,
"222.20": 222.20,
"-222.20": -222.20,
"+222,20": 222.20,
"-222,20": -222.20,
"-2.222,20": -2222.20,
"-11.111,20": -11111.20,
};
Run Code Online (Sandbox Code Playgroud)
欢迎提出建议。
小智 5
这是一个自给自足的 JS 函数,可以解决大多数欧洲/美国语言环境的这个(和其他)问题(主要是美国/德国/瑞典数字分块和格式之间......如OP中所示)。我认为这是对 Slawa 解决方案的改进(并受到其启发),并且没有依赖性。
function realParseFloat(s)
{
s = s.replace(/[^\d,.-]/g, ''); // strip everything except numbers, dots, commas and negative sign
if (navigator.language.substring(0, 2) !== "de" && /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(s)) // if not in German locale and matches #,###.######
{
s = s.replace(/,/g, ''); // strip out commas
return parseFloat(s); // convert to number
}
else if (/^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(s)) // either in German locale or not match #,###.###### and now matches #.###,########
{
s = s.replace(/\./g, ''); // strip out dots
s = s.replace(/,/g, '.'); // replace comma with dot
return parseFloat(s);
}
else // try #,###.###### anyway
{
s = s.replace(/,/g, ''); // strip out commas
return parseFloat(s); // convert to number
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案,没有任何依赖项:
return value
.replace(/[^\d\-.,]/g, "") // Basic sanitization. Allows '-' for negative numbers
.replace(/,/g, ".") // Change all commas to periods
.replace(/\.(?=.*\.)/g, ""); // Remove all periods except the last one
Run Code Online (Sandbox Code Playgroud)
(我省略了对数字的转换 -parseFloat如果您不关心 JavaScript 的浮点数精度问题,那可能只是一个调用。)
该代码假设:
| 归档时间: |
|
| 查看次数: |
141893 次 |
| 最近记录: |