简体   繁体   English

如何在javascript中显示小数点后两位数

[英]how to show two digits after decimal in javascript

I am using JSP for view.我正在使用JSP进行查看。 I am using angularjs and javascript我正在使用angularjsjavascript

I have a text box, in that text box there is a value called 12.542001 .我有一个文本框,在该文本框中有一个名为12.542001的值。 The ng-model should contain the same but when I am showing I want to show 12.54 only on the text box. ng-model应该包含相同的内容,但是当我显示时,我只想在文本框中显示12.54

If I work with toFixed(2) , Then it will round the value, but I don't want to round that value...如果我使用toFixed(2) ,那么它将舍入该值,但我不想舍入该值...

Try this尝试这个

 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));

Update更新

If your ng-model name is $scope.totalnumber .如果您的 ng-model 名称是$scope.totalnumber then try this然后试试这个

$scope.totalnumber = parseFloat(Math.round($scope.totalnumber * 100) / 100).toFixed(2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM