简体   繁体   中英

how to show two digits after decimal in javascript

I am using JSP for view. I am using angularjs and javascript

I have a text box, in that text box there is a value called 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.

If I work with toFixed(2) , Then it will round the value, but I don't want to round that value...

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 . then try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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