Andreas Lyngstad
Andreas Lyngstad

Reputation: 4927

Need present whole number with 2 decimals (5.00) in javascript

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.

Upvotes: 3

Views: 3406

Answers (4)

Dor Cohen
Dor Cohen

Reputation: 17080

You can use toFixed() method:

var num = num.toFixed(6);

Now num wil be equal to 6.00

Upvotes: 1

Alnitak
Alnitak

Reputation: 339856

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

Upvotes: 7

jAndy
jAndy

Reputation: 236032

Use Number.prototype.toFixed()MDN.

(Math.round((Math.abs(21600 / 3600))*100)/100).toFixed( 2 );

Upvotes: 2

robertklep
robertklep

Reputation: 203379

Try this:

(Math.round((Math.abs(21600 / 3600))*100)/100).toFixed(2)

Upvotes: 1

Related Questions