Hacker News new | ask | show | jobs
by sam_goody 1670 days ago
The comment on Numeric values is misleading:

> Use the _ operator to format numeric values:

> $price = 100_10;

> // $100 and 10 cents

Makes it sound like the underscore replaces the decimal, which it doesn't. The underscores are simply ignored by the PHP parser, so that 10_0.1 === 1_00.1.

You could afterwards use PHP to replace the `_` with a comma for display purposes, and I assume that is what they are trying to say.

2 comments

Yeah, that's a terrible example since some places do use comma as a dollars/cents separator, and others don't. And some people use floats for currency (with the obvious caveats), and might read this as "_ is the same as a decimal point".

They really should just use a simpler example, like 10_000_000 being easier to read as "10 million" than 10000000.

  > They really should just use a simpler example, like 10_000_000 being easier to read as "10 million" than 10000000.
Declarations such as `$million = 1000 * 1000;` or `$seconds_in_day = 24 * 3600;` are very common, and in my opinion more readable than the ignored-underscore syntax.
Perhaps, though the underscore syntax works in Perl, Ruby, Java, etc.

And the context here is how to explain it, not whether it should exist and/or be used.

Yea, Credo[1], one of the static analysis tools for Elixir specifies it as:

  Numbers can contain underscores for readability purposes.
  These do not affect the value of the number, but can help read large numbers
  more easily.

      141592654 # how large is this number?

      141_592_654 # ah, it's in the hundreds of millions!

  Like all `Readability` issues, this one is not a technical concern.
  But you can improve the odds of others reading and liking your code by making
  it easier to follow.
Should make it more obvious that it is purely a readability thing, and that is obviously subjective anyway

[1] https://github.com/rrrene/credo/blob/master/lib/credo/check/...