Thanks to visit codestin.com
Credit goes to github.com

Skip to content

AbstractDumper: decimalPoint fails for pt-BR #46156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rentalhost opened this issue Apr 24, 2022 · 11 comments
Closed

AbstractDumper: decimalPoint fails for pt-BR #46156

rentalhost opened this issue Apr 24, 2022 · 11 comments

Comments

@rentalhost
Copy link

Symfony version(s) affected

6.0.6

Description

When I dd(12.5), for instance, it is presented like float 12.5,0.

How to reproduce

dd(12.5)

Possible Solution

I can't help with a definitive solution, but I research where the problem is started, and is basically from AbstractDumper constructor, that depends on localeconv() information. The float to string conversion occur in CliDumper::dumpScalar().

For me, it returns:

array (size=18)
  'decimal_point' => string ',' (length=1)
  ...

The float dumper is something like:

            case 'double':
                $style = 'num';

                if (isset($this->styles['float'])) {
                    $style = 'float';
                }

                switch (true) {
                    case \INF === $value:  $value = 'INF'; break;
                    case -\INF === $value: $value = '-INF'; break;
                    case is_nan($value):  $value = 'NAN'; break;
                    default:
                        $value = (string) $value;
                        if (!str_contains($value, $this->decimalPoint)) {
                            $value .= $this->decimalPoint.'0';
                        }
                        break;
                }
                break;

But (string) $value returns string '12.5', that is incompatible to localeconv() info.

My suggestion is detect separator based on a float info directly. For instance:

$this->decimalPoint = str_replace('1', '', (string) 1.1); // => "."

Additional Context

No response

@rentalhost
Copy link
Author

image

@stof
Copy link
Member

stof commented Apr 25, 2022

Which version of PHP are you using ?

@stof
Copy link
Member

stof commented Apr 25, 2022

And which locale is configured on your system ? (the PHP locale as set through setlocale, not the Symfony locale).
You can do dump(setlocale( LC_NUMERIC, '0')); to get it.

@rentalhost
Copy link
Author

@stof I am running PHP 8.1.1, Windows 11. The locale is Portuguese_Brazil.1252.

@rentalhost
Copy link
Author

@stof while you asked, I remembered that PHP 8 performs float to string cast with locale-independent. So the . (dot) will always be used as separator. Then the rule can be even simpler:

$this->decimalPoint = PHP_VERSION_ID >= 80000
    ? '.'
    : str_replace('1', '', (string) 1.1);

https://wiki.php.net/rfc/locale_independent_float_to_string
https://php.watch/versions/8.0/float-to-string-locale-independent

@nicolas-grekas
Copy link
Member

Good call, can you please send a PR on branch 4.4?

@nicolas-grekas
Copy link
Member

To be independent from the float precision, the check should use substr instead of str_replace, see https://3v4l.org/fkpmo

@stof
Copy link
Member

stof commented Apr 25, 2022

Well, for PHP 7, the usage of localeconv can be kept instead of trying to do a hack to find it from the string representation of 1.1. The rule is simple: on PHP 8+ it is always the . and on PHP 7- it is always the value provided by localeconv

@nicolas-grekas
Copy link
Member

$this->decimalPoint = \PHP_VERSION_ID >= 80000 ? '.' : localeconv()['decimal_point'];

@rentalhost
Copy link
Author

@stof @nicolas-grekas If it's possible for any of you, please do a PR. I don't have the project here.

nicolas-grekas added a commit that referenced this issue Apr 25, 2022
This PR was merged into the 4.4 branch.

Discussion
----------

[VarDumper] Fix dumping floats on PHP8

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #46156
| License       | MIT
| Doc PR        | -

Commits
-------

472072e [VarDumper] Fix dumping floats on PHP8
@rentalhost
Copy link
Author

Checked! Now all is working fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants