...

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



Hamza7
07-07-2011, 04: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, 07: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, 08:41 PM
num.toFixed()*1;

Kor
07-08-2011, 11:02 AM
num.toFixed()*1;


//or
num.toFixed()-0;
//or
num.toFixed()/1;
:D

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

+num.toFixed()

Kor
07-08-2011, 06: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 ... :)

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum