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
Add a ment  | 

4 Answers 4

Reset to default 7

You 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