2

When I call the toFixed() method on a decimal number literal like so:

var a = 67.678.toFixed(2);
console.log(a);

The result works and returns 67.68

However if I call the method on an integer - I get an error

var b = 67.toFixed(2);
console.log(b); // causes ERROR

Why is this the case?

NB:

If I save the integer number to a variable - the toFixed() method does work.

var c = 67;
c = c.toFixed(2);
console.log(c); // returns 67.00

See this jsBin

What is going on under the hood?

Danield
  • 121,619
  • 37
  • 226
  • 255
  • 1
    Did you tried `(67).toFixed(2)`? –  Oct 14 '15 at 07:37
  • 3
    well Its a language related syntax issue. when you write `xx.` if `xx` is number it assumes the next thing to appear after the `.` operator is another number. but if you enclose it in parans like `(xx).toFixed(2)` it works. – Minato Oct 14 '15 at 07:37
  • So why does the decimal number not need parenthesis? – Danield Oct 14 '15 at 07:39
  • Well I've studied the Programming Languages construction course. these are syntax related rules. when a statement appears a set of grammar rules parses it and validates it.. Probably in the JS Grammar rules you can't put `number.function` and it return a parse/Syntax error – Minato Oct 14 '15 at 07:46

3 Answers3

6

var b = 67.toFixed(2); Simply generates a parsing error as the parser can't deduce that you meant it to be a number literal followed by a property accessor (Notice that the error is on the first line, not on the console.log(b))

The reason this works for 67.678.toFixed(2) is that there's no other option. The parser can deduce without ambiguity that the number literal ended at the "8" digit and can continue parsing the next dot as a property accessor (which causes boxing into a Number object first BTW).

A solution is obviously simple:

(67).toFixed(2);
Amit
  • 45,440
  • 9
  • 78
  • 110
3

Your options include:

67 .toFixed(2)
(67).toFixed(2)
67..toFixed(2)
67.0.toFixed(2)
67["toFixed"](2)

All of these avoid the problem that the JS parser treats a dot immediately following a number as a decimal point.

1

well Its a language related syntax issue. when you write xx. if xx is number it assumes the next thing to appear after the . operator is another number. but if you enclose it in parenthesis like (xx).toFixed(2) it works. what happens in a the back is an object after the parenthesis delimits the parse or when a full decimal literal is written is created and toFixed is called on that object.

Hope it answers our question.. :)

Minato
  • 4,383
  • 1
  • 22
  • 28
  • 1
    No, the parentheses do not "create an object". They delimit the number for purposes of parsing. –  Oct 14 '15 at 07:45
  • Though it may be a possibility but I guess an object is created. to link the `toFixed` method call. since the definition of the method might not be in the syntax or semantic rules of JS language – Minato Oct 14 '15 at 07:49
  • Yes, a transient object is created in order to invoke `toFixed`. However, such a object is created in any case, including `67.0.toFixed()`. You stated the parentheses create the object. That is incorrect. –  Oct 14 '15 at 07:50