这是我的 JavaScript:
var total = $(this).text();
var calc = total * 100;
var paid = calc * 0.333;
HTML 很简单:
<div class="price"><span>£0.21</span></div>
如何从字符串中删除“£”字符以便进行数学计算?
最佳答案
这将从字符串中删除所有非数字或小数点的字符。
var total = $(this).text().replace(/[^\d.]/g,'');
var calc = total * 100;
var paid = calc * 0.333;
如果还有逗号分隔符,则很有用。
*
将负责字符串的转换。如果您想要更明确的内容,可以使用一元 +
运算符。
var total = +($(this).text().replace(/[^\d.]/g,'')); // <-- has + at beginning
var calc = total * 100;
var paid = calc * 0.333;
关于JavaScript 数学不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4586213/