如何用点和逗号解析数字
How to parse number with points and commas
我收到了这样的字符串;
1.234.567,89
我要1234567.89
逗号是十进制delimiter.Point是千位,百万分隔符
我想当成一个数字。
我尝试替换,但只适用于第一个“.”。和解析浮点数。
我也尝试了一些在这里找到的正则表达式,但对我不起作用
我想要这个;
var numberAsString= '1.234.567,89';
//Step to clean string and conver to a number to compare (numberAsString => numberCleaned)
if (numberCleaned> 1000000) {alert("greater than 1 million");}
有什么想法吗?
(很抱歉,如果这是一个新手问题,但我在几个小时内没有找到任何解决方案...)
您可以将 replace
与 g
一起使用
const val = '1.234.567,89'.replace(/\./gi, '').replace(/,/, '.');
console.log(val)
console.log(typeof parseFloat(val))
这应该适用于当前场景。
1st 删除点然后用点替换逗号。
let number = "1.234.567,89";
function parseNum(num){
return num.replace(/\./g, '').replace(",", ".")
}
console.log(parseNum(number));
我收到了这样的字符串;
1.234.567,89
我要1234567.89
逗号是十进制delimiter.Point是千位,百万分隔符
我想当成一个数字。
我尝试替换,但只适用于第一个“.”。和解析浮点数。 我也尝试了一些在这里找到的正则表达式,但对我不起作用
我想要这个;
var numberAsString= '1.234.567,89';
//Step to clean string and conver to a number to compare (numberAsString => numberCleaned)
if (numberCleaned> 1000000) {alert("greater than 1 million");}
有什么想法吗? (很抱歉,如果这是一个新手问题,但我在几个小时内没有找到任何解决方案...)
您可以将 replace
与 g
const val = '1.234.567,89'.replace(/\./gi, '').replace(/,/, '.');
console.log(val)
console.log(typeof parseFloat(val))
这应该适用于当前场景。 1st 删除点然后用点替换逗号。
let number = "1.234.567,89";
function parseNum(num){
return num.replace(/\./g, '').replace(",", ".")
}
console.log(parseNum(number));