【问题标题】:ionic2 angular2 sum up value in a columnionic2 angular2 对列中的值求和
【发布时间】:2017-11-06 21:44:41
【问题描述】:

我有一个向用户显示发票的应用程序,除了发票中所列项目的总价值外,我已经设置了所有内容,我使用以下代码对价值求和,但我没有得到 sum ,而是得到了连接值

this.http.post('http://myurl',data,headers)
.map(res => res.json())
.subscribe(res => {
console.log(res)
 loader.dismiss()
this.items=res.server_response;

  this.totalAmount = this.getTotal();


});
});
 }
;
}

getTotal() {
    let total = 0;
    for (var i = 0; i < this.items.length; i++) {
        if (this.items[i].amount) {
            total += this.items[i].amount;
            this.totalAmount = total;
        }
    }
    return total;
}

显示值为 0100035004000 假设为 (1000+3500+4000)

【问题讨论】:

标签: angular ionic2


【解决方案1】:

total += this.items[i].amount; 似乎将数字连接为字符串,而不是将其更改为 total += Number(this.items[i].amount); 以在 typescript 中从字符串转换为数字。

【讨论】:

    【解决方案2】:

    通过添加 + 将 items.amount 解析为数字:

    getTotal() {
     let total = 0;
     for (var i = 0; i < this.items.length; i++) {
        if (this.items[i].amount) {
            total += +this.items[i].amount;
            this.totalAmount = total;
        }
     }
     return total;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2017-02-16
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 2017-09-07
      • 2017-02-15
      相关资源
      最近更新 更多