我能够将数字转换为逗号分隔的语言环境字符串。是否有类似的函数将逗号分隔的区域设置字符串转换为数字?
var number = 1000000;
number.toLocaleString();
Output: "1,000,000"
如何使用JavaScript将1,000,000转换为1000000?
答案 0 :(得分:2)
var number = 1000000;
console.log('numeric number', number); //1000000
var str = number.toLocaleString();
console.log('with commas', str); //1,000,000
var num = str.replace(/\D/g,'');
console.log('string without commas', num); //1000000 (string)
var num2 = parseInt(num);
console.log('numeric', num2); //1000000 (numeric)
将可能包含逗号的字符串转换为数字的简单方法是在字符串中查找任何非数字并将其删除,因此只保留数字。
var num = number.toLocaleString().replace(/\D/g,''); //1000000