javascript - Angular 显示带有额外零的数字

标签 javascript angular

我试图显示两个数字的总和,但它返回额外的零。

示例

newprice -> 174.000
added number -> 1
total should be -> 174.001

but it shows 1.740.001

代码

查看

{{(newprice + optionPrice | currency: 'Rp ' : 'symbol' : '1.0-0')}}

Controller

export class OptionsPage implements OnInit {
  optionPrice = '0';
  newprice: null;

  constructor(
    public modalController: ModalController
  ) { }

  ngOnInit() {
    this.newprice; // it comes from other page in this case is (174000)
    this.optionPrice;
  }

// this price comes from selected option and will be sum to "newprice" value (in this sample is 1)
  onChange(value) {
    console.log(value);
    this.optionPrice = value;
  }
}

有什么想法吗?

最佳答案

我认为它的作用就像newpriceoptionPrice是字符串。

你可以这样做:

{{((+newprice) + (+optionPrice) | currency: 'Rp ' : 'symbol' : '1.0-0')}}
//  ^             ^    this "+" operators converts strings to number

关于javascript - Angular 显示带有额外零的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57787388/

相关文章:

javascript - javascript 中的地址字符串拆分

javascript - 如何解决 "module is not defined"错误?

javascript - 当我在图标 Bootstrap 中单击时展开网格系统列

css - 无法在 Angular 中选择绑定(bind)属性

javascript - 复选框的选中属性未按预期工作

显示昨天和今天日期的 Javascript 代码

javascript - 使用带有折叠菜单的 jQuery

node.js - 解析器中的 Angular HttpClient 问题

Angular 6 : RxJS switchMap operator is not working as expected

javascript - 如何在浏览器存储中存储(类类型)对象并检索它?