View Full Version : Number.toFixed() does it convert number into string?

07-07-2011, 05:07 PM
When I used toFixed() method on a number, I thought that this method round a number to a specified approximation, but I got a surprising result, the number became string!
15.23689.toFixed(2) ==> "15.24"
So does it convert the number into string?

Logic Ali
07-07-2011, 08:22 PM
It returns a string representing the number after rounding, which is useful for representation purposes without losing the original value. It has no effect upon the number.

One way to get a number type to hold the rounded number: var n = Number( 15.23689.toFixed(2) );

rnd me
07-07-2011, 09:41 PM

07-08-2011, 12:02 PM


rnd me
07-08-2011, 06:22 PM
an even simpler coercion:


07-08-2011, 07:19 PM
That's a good one :) But most of the time I prefer Number(). It is more intuitive for other coders from the team which might need to analyze or change the codes. The easiness of debugging, you know ... :)

07-08-2011, 08:07 PM
I get debuggers out of my nose every morning.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum