views:

58

answers:

3

I wanted to display a number with 2 decimal places.

I thought I could use toPrecision(2) in JavaScript .

However, if the number is 0.05, I get 0.0500. I'd rather it stay the same.

What is the best way to do this?

I can think of coding a few solutions, but I'd imagine (I hope) something like this is built in?

Thanks

Update

See it on JSbin.

+1  A: 

You could to it with the toFixed function, but it is buggy in IE. If you want a reliable solution, look at my answer here.

mikez302
+3  A: 

Try toFixed instead of toPrecision.

casablanca
You're on fire today casablanca!
alex
+1  A: 

Try float_num.toFixed(2);

You'll need to verify the rounding.

Jason McCreary