javascript - javascript添加数字
问题描述
我有一个功能
function updateLensTotals() {
var lensesTotalIs = 0;
for (z = 0; z < lensesArray.length; z += 7) {
if (lensesArray[z + 2]) {
console.log(z + " lensesArray[z+2] is: " + lensesArray[z + 2] + " | ");
if (isNaN(lensesArray[z + 2])) {
console.log(lensesArray[z + 2] + " is not a number <br/>");
} else {
console.log(lensesArray[z + 2] + " is a number <br/>");
}
lensesTotalIs = parseFloat(lensesTotalIs) + lensesArray[z + 2];
if (isNaN(lensesTotalIs)) {
console.log(lensesTotalIs + " is not a number <br/>");
} else {
console.log(lensesTotalIs + " is a number <br/>");
}
console.log(z + " lensesTotalIs: " + lensesTotalIs + " | ");
}
}
jQuery('#lensesTotalDue').html("£" + parseFloat(lensesTotalIs));
return;
}
在控制台中产生这个
0 lensesArray[z+2] is: 19.99 |
19.99 is a number <br/>
019.99 is a number <br/>
0 lensesTotalIs: 019.99 |
cashPaid 0 chequePaid 0 cardPaid 0
0 lensesArray[z+2] is: 19.99 |
19.99 is a number <br/>
019.99 is a number <br/>
0 lensesTotalIs: 019.99 |
7 lensesArray[z+2] is: 15.99 |
15.99 is a number <br/>
19.9915.99 is not a number <br/>
7 lensesTotalIs: 19.9915.99 |
有一种形式提供产品的价值。我第一次使用 19.99,第二次使用 19.99 和 15.99。
第一次运行一切正常。19.99 是一个数字,很容易添加到总数中。但在第二次运行中,总似乎突然被视为文本流。我一定是在做一些愚蠢的事情,但我看不到。
谢谢
解决方案
推荐阅读
- java - 您如何将单个单词匹配为只有一个单词的字符串中输入句子中的单独单词
- c# - 在 Xamarin 表单项目中实现谷歌身份验证代码(不是通过谷歌身份验证)
- php - 如何将我的 ReactJS Web 应用程序部署到生产服务器
- react-native - React Native:更改 Paper Button 中的文本颜色
- php - Opencart 中没有组成子类别
- javascript - Svelte 是否取消了 MVC?
- python - 如果这些项目是蓝莓,我如何找到字典中项目的平均价格?
- c# - 在其他程序集中加载控件时,TextBox 的样式会导致异常
- sql - 基于匹配值的雪花SQL计数和从另一个表求和
- python - 正则表达式检查是否没有空词不是特定词