首页 > 解决方案 > 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 是一个数字,很容易添加到总数中。但在第二次运行中,总似乎突然被视为文本流。我一定是在做一些愚蠢的事情,但我看不到。

谢谢

标签: javascriptnumbers

解决方案


推荐阅读