Angular:toFixed() 适用于一个值但不适用于另一个值?

Angular : toFixed() works with one value but not with the other?

我正在使用具有 2 个输入的表单。他们每个人都调用calculeSalaire()函数。

calculeSalaire()
{
  this.fraisGestion = this.getFraisGestion();
  this.tauxFraisGestion = this.getTauxFraisGestion();
  this.salaireBrut = this.getSalaireBrut();
  this.salaireNet = this.getSalaireNet();
  this.chargesSalariales = this.getChargesSalariales();
  this.chargesPatronales = this.getChargesPatronales();
  this.totalPaye = this.getTotalPaye();
}

所有这些函数 return number。我只想为每个值显示 2 个小数位,这就是我使用 toFixed(2) 函数的原因。

它适用于我的所有功能,除了这个:

getSalaireBrut()
{
   this.salaireBrut = (this.chiffreAffaire - this.fraisGestion - 
   this.fraisPro)/(1.10)/(1.447);

   var salaireBruttxt = this.salaireBrut.toFixed(2);

   return parseFloat(salaireBruttxt);
}

chiffreAffaire 是一个输入

fraiPro 是一个输入

fraisGestion计算

我不明白为什么它对前面的函数不起作用,而它在下面的函数中起作用。

getChargesSalariales()
  {
    this.chargesSalariales = this.salaireBrut*(23/100);
    var chargesSalarialtxt = this.chargesSalariales.toFixed(2);

    return parseFloat(chargesSalarialtxt);
  }

您可以注意到我使用了 salaireBrut 值,该值是由不起作用的函数计算得出的值。

你知道为什么会这样吗?

toFixed 函数 return 是一个字符串,因为数字永远不会保留或显示尾随零。因此, toFixed 实际上在您的函数中完全正常工作(您可以在此处放置断点时轻松检查它),但是 parseFloat 再次使它成为一个数字,它永远不会包含尾随正如我之前所说的零。

所以你实际上必须 return 一个字符串 - 删除 parseFloat 将解决问题。