[英]Using toFixed to get a decimal number for a number field
I have this field 我有这个领域
<input type="number" name="amount" step="0.01" ng-model="amount" />
$scope.amount
is a variable with the value in it which is a string so I do this: $scope.amount
是一个变量,其值是一个字符串,所以我这样做:
$scope.amount = parseInt($scope.amount).toFixed(2);
This does return the value of let's say 0.00
but it is in a string but it won't populate the field as it is only a number field and toFixed();
这确实返回了例如
0.00
的值,但是它在字符串中,但是由于只有数字字段和toFixed();
而不会填充该字段toFixed();
is returning a string even though I still parsed it. 返回字符串,即使我仍然解析它。
What is a way around this? 有什么办法解决这个问题?
use parseFloat $scope.amount = parseFloat(parseInt($scope.amount).toFixed(2)); 使用parseFloat $ scope.amount = parseFloat(parseInt($ scope.amount).toFixed(2));
toFixed() converts the number to string check here https://www.w3schools.com/jsref/jsref_tofixed.asp toFixed()将数字转换为字符串在这里检查https://www.w3schools.com/jsref/jsref_tofixed.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.