On 27.12.2018 at 17:33, Umberto Salsi wrote:

> cmbecke...@gmx.de ("Christoph M. Becker") wrote:
> 
>> Regarding the decimal separator (aka. decimal point), the behavior of
>> casting float to string is inconsistent with casting string to float.
>> [...]
> 
> I'm shocked... Lot of code here assumes (float) does the exact reverse of
> (string); forunately most servers are configures with the default locale
> "C", but I'm still concerned for libraries portability.
> 
> Did you filed a bug for that?

There is <https://bugs.php.net/77278>.

> Anyway, here is some test code I wrote:
> […]
> Output:
> FAILED with LC_NUMERIC=de, (string) 0.5: got 0,5, exp 0.5
> FAILED with LC_NUMERIC=de, sprintf("%f", 0.5): got 0,500000, exp 0.500000
> 
> The second failed test is expected, as printf, scanf and co. are intended
> to be locale-aware.
> 
> But the first one? Is there any simple, efficient and safe way to convert
> back a string into float?

Well, if you have the appropriate locale set, you can use scanf() with
%f.  If you don't want to rely on the per process locale, you could use
NumberFormatter::parse() instead.  filter_var() with
FILTER_VALIDATE_FLOAT can also be used, and might be the best option if
you don't know which decimal separator is used, and you are sure there
are no thousands separators in the string.

-- 
Christoph M. Becker

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to