呈現整個數與2位小數(5.00)我有此需要在javascript
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
我需要整數2位小數。
呈現整個數與2位小數(5.00)我有此需要在javascript
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
我需要整數2位小數。
您可以使用.toFixed()
,但手動沒有必要一輪值精確到0.01第一 - 在.toFixed
功能會爲你做的。
var str = Math.abs(21600/3600).toFixed(2);
試試這個:
(Math.round((Math.abs(21600/3600))*100)/100).toFixed(2)
使用Number.prototype.toFixed()
MDN。
(Math.round((Math.abs(21600/3600))*100)/100).toFixed(2);
太棒了! 3分鐘內出現5個答案 – 2013-04-30 11:03:03
[Format number to always show 2 decimal places]的可能重複(http://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) – Curt 2013-04-30 11:13:16
Alnitak's答案使這是一個很好的交談。他的回答也大大清理了代碼! – 2013-04-30 11:32:09