我正在尝试将一个变量的格式设置为始终具有2个小数,但是由于以下原因,我无法获取该方法-它总是删除结尾的0或说:
“字符串”类型不能分配给“数字”类型
viewer_gain
变量是数字类型,因此会产生问题,我可以使用带2位小数的字符串,但不能包含数字...
这就是我正在做的:
setCompensation() {
// set these variables to string, to use toFixed(2)
let value: string;
let zero: string;
if (isNaN(this.campaignModel.per_view_pay) || !this.compensateV) {
// since toFixed is converting to string, i am trying to parseFloat later.
zero = (0).toFixed(2);
// removes the 0 at the end here
this.campaignModel.viewer_gain = parseFloat(zero);
} else {
// since toFixed is converting to string, i am trying to parseFloat later.
value = (this.campaignModel.per_view_pay / 2).toFixed(2);
// removes the 0 at the end here
this.campaignModel.viewer_gain = parseFloat(value);
}
}
有人用另一种方式吗?