Kic*_*chu 36 javascript jquery
这些是我的字符串:
"test123.00"
"yes50.00"
Run Code Online (Sandbox Code Playgroud)
我想用50.00添加123.00.
我怎样才能做到这一点?
我使用了命令parseInt()但它在警告框中显示NaN错误.
这是代码:
str1 = "test123.00";
str2 = "yes50.00";
total = parseInt(str1)+parseInt(str2);
alert(total);
Run Code Online (Sandbox Code Playgroud)
Pra*_*ana 88
只要这样做,你需要删除"数字"和"."以外的字符.形成你的字符串将为你工作
yourString = yourString.replace ( /[^\d.]/g, '' );
Run Code Online (Sandbox Code Playgroud)
你的最终代码将是
str1 = "test123.00".replace ( /[^\d.]/g, '' );
str2 = "yes50.00".replace ( /[^\d.]/g, '' );
total = parseInt(str1, 10) + parseInt(str2, 10);
alert(total);
Run Code Online (Sandbox Code Playgroud)
要使parseInt起作用,您的字符串应该只包含数字数据.像这样的东西:
str1 = "123.00";
str2 = "50.00";
total = parseInt(str1)+parseInt(str2);
alert(total);
Run Code Online (Sandbox Code Playgroud)
你可以在开始处理它们之前拆分它吗?
归档时间: |
|
查看次数: |
107078 次 |
最近记录: |