我正在使用 JSP
进行查看。我正在使用 angularjs
和 javascript
我有一个文本框,在该文本框中有一个名为 12.542001
的值。 ng-model
应该包含相同的内容,但是当我显示时,我只想在文本框中显示 12.54
。
如果我使用 toFixed(2)
,那么它会将值四舍五入,但我不想对该值进行四舍五入...
最佳答案
试试这个
console.log(parseFloat(Math.round(12.542001 * 100) / 100).toFixed(2));
//or use
num = "122.542001";
var spV= num.split(".");
console.log(spV[0] +"."+ spV[1].substring(0,2));
更新
如果你的 ng-model 名称是 $scope.totalnumber
。然后试试这个
$scope.totalnumber = parseFloat(Math.round($scope.totalnumber * 100) / 100).toFixed(2)
关于javascript - 如何在javascript中显示小数点后两位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49146055/