1

I tried with the example value x = 123,i want two precision. So, i use x.toFixed(2). Then i will get the output "123.00" but i want the output as 123.00 which is floating number with decimals.

var x = 123
x.toFixed(2);
output: "123.00"
expected: 123.00

enter image description here

Sandeep Bhaskar
  • 300
  • 2
  • 12

1 Answers1

3

A floating point notation or decimal number is not something explicitly declared. When the decimal point has nothing after the point, i.e., .0 It becomes an integer.

The .toFixed() is just for aesthetic purposes only. It also helps you to rounds off to the number of decimals too.

2.50000 and 2.5 are the exact same number. If you want to keep trailing zeroes, you'll have to use a string.

When I try to do this on my Chrome Console:

You can see that even when you do a strict type-checking, the decimals are considered like comments by the JavaScript parser. It might be a bit unclear to understand for developers coming from statically typed or strongly typed languages like Java & C#, where you can have separate float and double types.

Related: JavaScript - Keep trailing zeroes.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183857/discussion-between-sandeep-bhaskar-and-praveen-kumar-purushothaman). – Sandeep Bhaskar Nov 18 '18 at 19:13