我真的不明白 typescript 中变量的类型分配系统是如何工作的。 谁能帮助我为什么这不起作用。 谢谢
valorParcela:number;
totalCost:number;
VendaProdutoVendaNParcelas: number;
this.valorParcela = Number( this.totalCost / this.VendaProdutoVendaNParcelas).toFixed(2);
我期望一个简单的parseFloat
最佳答案
toFixed
返回一个字符串
,您将其分配给一个应为数字
的变量。您需要另一对 ()
将 toFixed
的结果转换回数字
this.valorParcela = Number(( this.totalCost / this.VendaProdutoVendaNParcelas).toFixed(2));
或者您可以使用一元 +
技巧来执行数字转换:
this.valorParcela = +(this.totalCost / this.VendaProdutoVendaNParcelas).toFixed(2);
关于angular - 类型 'string' 不可分配给类型 'number',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56394034/