That’s how it works:
console.log((2).toString());
It is a matter of syntax ambiguity of the language. JS allows a number to be expressed as decimal without filling the decimal part, that is, allows you to put only the integer number and a point and nothing else, which would create ambiguity for the call of the method starting with point.
console.log(2.);
console.log((2.).toString());
console.log(2.5.toString());
How would it be right? Ignore the decimal point? It seems very wrong. Use twice the dot? Strange and would prevent a syntax using this pattern (crease), then it was preferred to require that literal numbers could only be called by methods within parentheses or with space between the number and the point of the method (see credit in the comment below).
console.log(2. .toString());
console.log(2 .toString());
//console.log(2.toString());
//console.log(2..toString());
A mistake was made in the language to correct another error, only Javascript had chosen to require that the decimal part always be written explicitly, even if it is only a 0. It does not require, but you can do so and avoid major problems, so my recommendation is always do so for your case, it seems to me the most intuitive and matches with other languages:
console.log(2.0.toString());
And according to the issue in the question, the toFixed()
also does not allow, the problem is ambiguous syntax and nothing with respect to the method itself or object it belongs to. Note that if you use a full decimal (integer plus decimal part, you can use without problems):
console.log(2.5.toString());
//console.log(2.toFixed());
I put in the Github for future reference.
So in case it didn’t work because he considered the point a decimal separator and waited another number after him? Very interesting, thank you for the explanation.
– Máttheus Spoo