I noticed that when calling toFixed
against a negative exponential number, the result is a number, not a string.
First, let's take a look at specs.
Number.prototype.toFixed (fractionDigits)
Return a
String
containing this Number value represented in decimal fixed-point notation with fractionDigits digits after the decimal point. If fractionDigits isundefined
,0
is assumed.
What actually happens is (tested in Chrome, Firefox, Node.js):
> -3e5.toFixed()
-300000
So, the returned value is -3e5
. Also, notice this is not a string. It is a number:
> x = -3e5.toFixed()
-300000
> typeof x
'number'
If I wrap the input in parentheses it works as expected:
> x = (-3e5).toFixed()
'-300000'
> typeof x
'string'
Why is this happening? What is the explanation?