admin管理员组文章数量:1302272
I have this
Math.round((Math.abs(21600 / 3600))*100)/100
>> 6 # want 6.00
Math.round((Math.abs(21000 / 3600))*100)/100
>> 5.83 # This is right
I need 2 decimals on whole number.
I have this
Math.round((Math.abs(21600 / 3600))*100)/100
>> 6 # want 6.00
Math.round((Math.abs(21000 / 3600))*100)/100
>> 5.83 # This is right
I need 2 decimals on whole number.
Share asked Apr 30, 2013 at 10:55 Andreas LyngstadAndreas Lyngstad 4,9272 gold badges38 silver badges70 bronze badges 3- Great! 5 answers in 3 minutes – Andreas Lyngstad Commented Apr 30, 2013 at 11:03
- possible duplicate of Format number to always show 2 decimal places – Curtis Commented Apr 30, 2013 at 11:13
- Alnitak's answer makes this a good conversation. His answer also cleans up the code considerably! – Andreas Lyngstad Commented Apr 30, 2013 at 11:32
4 Answers
Reset to default 7You can use .toFixed()
, but there's no need to manually round the value to the nearest 0.01 first - the .toFixed
function will do that for you.
var str = Math.abs(21600 / 3600).toFixed(2);
Use Number.prototype.toFixed()
MDN.
(Math.round((Math.abs(21600 / 3600))*100)/100).toFixed( 2 );
Try this:
(Math.round((Math.abs(21600 / 3600))*100)/100).toFixed(2)
You can use toFixed() method:
var num = num.toFixed(6);
Now num wil be equal to 6.00
本文标签: Need present whole number with 2 decimals (500) in javascriptStack Overflow
版权声明:本文标题:Need present whole number with 2 decimals (5.00) in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741664936a2391252.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论