如何使用脚本将字符串(1,234)转换为数字(1234)

How can I convert a string(1,234) into a number(1234) using script?

本文关键字:转换 1234 数字 脚本 何使用 字符串      更新时间:2023-09-26

几种方法可以将字符串转换为整数,当字符串 ex:"1,234",

parseInt("1,234") 

转换为数字 O/P : 将为 1。当我输入字符串"1,234"时,有没有办法将数字获取为 1234。请建议我获取号码。提前谢谢。

我会从给定的字符串中删除所有逗号。

parseInt("1,234".replace(/,/g,""),10);

请注意,/,/g是必需的(而不是","因为

"1,234,567".replace(",","") == "1234,567";
"1,234,567".replace(/,/g,"") == "1234567";

请注意,"," 将仅替换 , 的第一个实例,而/,/g将替换所有实例。

带变量

var tempnum="1,234";
parseInt(tempnum.replace(/,/g,""));

无变量

parseInt("1,234".replace(/,/g,""));

引用替换

看看这个

var num = '1,234';
num = num.replace(/,/g, '');
num = parseInt(num, 10);