+5  A: 

Of course it returns a string. If you wanted to round the numeric variable you'd use Math.round() instead. The point of toFixed is to format the number with a fixed number of decimal places for display to the user.

Joel Coehoorn
@Derek: `Math.floor`.
KennyTM
+1  A: 

What would you expect it to return when it's supposed to format a number ? If you have a number you can't pretty much do anything with it because e.g.2 == 2.0 == 2.00 etc. so it has to be a string.

Thomas Wanner
A: 

Because its primary use is displaying numbers? If you want to round numbers, use Math.round() with apropriate factors.

Christoph
but it's displaying NUMBERS so shouldn't it return a "number"?
Derek Adair
@Derek: Only in the way that `'42'` is a number...which it's not. Just because a string happens to contain digits only does not make it a number. This isn't PHP. :-P
Chris Jester-Young
lol. It's not a string that contains a number... It's a number that is passed to a method. The method takes a number and returns a string.
Derek Adair
A: 

Number.prototype.toFixed is specified to work that way:

Number.prototype.toFixed(fractionDigits)

Return a string containing the number represented in fixed-point notation with fractionDigits digits after the decimal point. […]

Gumbo
+2  A: 

It returns a string because 0.1, and powers thereof (which are used to display decimal fractions), are not representable (at least not with full accuracy) in binary floating-point systems.

For example, 0.1 is really 0.1000000000000000055511151231257827021181583404541015625, and 0.01 is really 0.01000000000000000020816681711721685132943093776702880859375. (Thanks to BigDecimal for proving my point. :-P)

Therefore (absent a decimal floating point or rational number type), outputting it as a string is the only way to get it trimmed to exactly the precision required for display.

Chris Jester-Young
at least javascript could save me some finger-work and convert it back to a number... sheesh...
Derek Adair
@Derek: Yeah, but once you convert it back into a number, you run into the same inaccuracy issues again. :-P JS doesn't have decimal floating point or rational numbers.
Chris Jester-Young
thanks for the explanation
Derek Adair