Why suppress zero before the point in decimals, as in 0.5 == . 5?

Asked

Viewed 190 times

2

Sometimes I face a code where the programmer wrote a decimal without zero, like 0.5 being .5, for example:

var decimal1 = 0.5;
var decimal2 = .5;

console.log(decimal1 == decimal2);
console.log(decimal1, decimal2);

From my point of view, write a decimal as .5 leaves the reading impaired, so I always prefer to use it with zero: 0.5.

What really happens when we determine a decimal without zero before the point? Why can we define a decimal variable like this? Are there differences between the two ways? Is there any increase in performance?

  • Can anyone tell me why -1?

  • 1

    There is a technique, the fewer letters on a page, the less weight. But I don’t know if writing like this is worth it. Notice what javascript optimizers/reducers do with the page, some arrive "eat" part of code that I would be ashamed to write.

  • 2

    Not having "0" before does not negatively affect readability (in the general case). It is a widely accepted convention. Whether or not it turns out to be just a stylistic and cultural issue.

  • @Jeffersonquesado, yes. I made it clear that my point of view, is that why the negative votes? But why is there this stylistic question and why does language allow it? The .x exists for some reason?

  • 3

    You can even test on a homemade calculator, if you type . 5, it will understand that it is 0.5. I think it’s only by convention even, in college, I had a numerical calculus course, where I had a topic that taught the best rounding practices, where we used very tricky numbers, like 0.0089240544.... Ai at the time of writing on the calculator, the teacher himself recommended this form . 0089240544 because whether you like it or not, it becomes easier for us to write, and at the time of reading on the calculator display, it seems funny, but one less character ended up making it easier, and a lot!

  • @Marceloboni, cool. I’ve noticed that in other languages this happens too. The answer to this is beyond programming, then? I was even more curious... I had a teacher who said otherwise, even though we weren’t going to do a calculator bill, said to keep zero so we wouldn’t get lost in the bills.

  • I think it goes into the same case of numbers defined as 0x, 0b e 0o vine. The javascript interpreter uses some regex to detect what comes after the 0x, 0b, 0o and . to make the conversion calculation... note that if you write only . no number after, the console sends a syntax error.

  • @bio is standard that the recognition of floating point numbers is provided through something close to the expression ([1-9][0-9]*|0?)(\.[0-9]*)?

  • What worries me is that it works: console.log(parseFloat(".2fhh.95fgh/4")); // 0.2 for example

Show 4 more comments

1 answer

4


What really happens when we determine a decimal without zero before the point?

Nothing, this is a matter of syntax, does not change the execution, the meaning, nothing, has only one character less.

The number is the same, that’s just a representation of it on the screen in this context. Note that the writing on the screen is also just a textual representation. It was always agreed to put 0 before the point, which seems a little inconsistent.

JS is not known to have a strong strict syntax requirement.

Why can we define a decimal variable this way?

The simple and obvious answer is that language defined it like this. As there is no ambiguity they felt that it was not necessary to force something just for readability.

One hypothesis is to keep the language less verbose, although that would be questionable. They even talked about getting a smaller file, but it happens so few times that I doubt it would be a good motivation. If this were the case the language would have other things that allow the smaller text more intensely and with less readability problems.

Mathematics sort of defines zeros in the left integer of the number as meaningless, and so can be omitted. As well as the zeros on the right in the decimal part.

There are differences between the two ways?

Zero.

Is there any increase in performance?

Zero.

Do as you see fit, because it has little relevance.

  • When I created this question I imagined something different. After the comments I realized it didn’t make much sense. I thought the .5 existed for some reason, but thanks to you I know that it is a matter of syntax and is common in the mathematical medium. Thank you for the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.