Alex Rivera | Logout

Format number to always show 2 decimal places

Asked 2011-05-26T05:22:51.133
1315

I would like to format my numbers to always display 2 decimal places, rounding where applicable.

Examples:

number     display
------     -------
1          1.00
1.341      1.34
1.345      1.35

I have been using this:

parseFloat(num).toFixed(2);

But it's displaying 1 as 1, rather than 1.00.

Edit
Report

1 Answer

14

Simplest answer:

var num = 1.2353453;
num.toFixed(2); // 1.24

Example: http://jsfiddle.net/E2XU7/

answered 2013-04-08T18:37:07.913

Your Answer