Allow number_format() have three args

  105138
April 8, 2019 07:50 david.proweb@gmail.com (David Rodrigues)
Currently number_format() could accept one, two or four args. If three args
is passed then you will receive "wrong parameter count for number_format()"
error (ArgumentCountError).

It have four parameters: float $number, int $decimals, string $dec_point
and string $thousands_sep.

* If one arg is passed, then $decimals = 0 and $thousands_sep = ",".
* If two args is passed, then $dec_point = "." and $thousands_sep = ",".
* If four args is passed, then it will respect given args;

There some cases where we just don't need a $thousands_sep, so I suggest to
accept three args with $thousands_sep = '' (and not a comma).

It will works fine for the doc (
https://www.php.net/manual/en/function.number-format.php) example:

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', ''); // Currently
$english_format_number = number_format($number, 2, '.'); // After
// 1234.57 (same result)

The fourth parameter will change "the behavior" if compared with the
another original cases, but it will happen because there is no reason to
use three args if you pretends to keep the comma as fourth arg once that
normally you will use or "." or "," as decimal separator.

* If you pretends to use dot + comma, just use two args;
* If you pretends to use comma + dot, just use four args;
* If you pretends to use dot + nothing, use three args;
* If you pretends to use comma + nothing, use three args;

-- 
David Rodrigues
  105139
April 8, 2019 08:01 nikita.ppv@gmail.com (Nikita Popov)
On Mon, Apr 8, 2019 at 9:51 AM David Rodrigues proweb@gmail.com>
wrote:

> Currently number_format() could accept one, two or four args. If three args > is passed then you will receive "wrong parameter count for number_format()" > error (ArgumentCountError). > > It have four parameters: float $number, int $decimals, string $dec_point > and string $thousands_sep. > > * If one arg is passed, then $decimals = 0 and $thousands_sep = ",". > * If two args is passed, then $dec_point = "." and $thousands_sep = ",". > * If four args is passed, then it will respect given args; > > There some cases where we just don't need a $thousands_sep, so I suggest to > accept three args with $thousands_sep = '' (and not a comma). > > It will works fine for the doc ( > https://www.php.net/manual/en/function.number-format.php) example: > > // english notation without thousands separator > $english_format_number = number_format($number, 2, '.', ''); // Currently > $english_format_number = number_format($number, 2, '.'); // After > // 1234.57 (same result) > > The fourth parameter will change "the behavior" if compared with the > another original cases, but it will happen because there is no reason to > use three args if you pretends to keep the comma as fourth arg once that > normally you will use or "." or "," as decimal separator. > > * If you pretends to use dot + comma, just use two args; > * If you pretends to use comma + dot, just use four args; > * If you pretends to use dot + nothing, use three args; > * If you pretends to use comma + nothing, use three args
This seems very confusing. Despite the somewhat convoluted description in the docs, the way the function currently works is with the number_format(float $number, int $decimals = 0, string $dec_point = ".", string $thousands_sep = ",") signature and all defaults behaving as usual, plus an explicit check to prohibit three arguments. I think it would make sense to allow three arguments to make this function behave more normally, but in this case "," should stay the default of the thousands separator. Changing a default value based on the number of arguments passed would be a major WTF moment, imho. I don't think think the confusion is worth saving an "" argument. Regards, Nikita