#48315 [Com]: Type conversion into a float respects the numeric settings of the locale
| From: | carsten_sttgt at gmx dot de | Date: | Mon, 18 May 2009 18:25:32 +0000 |
| Subject: | #48315 [Com]: Type conversion into a float respects the numeric settings of the locale | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-137450@lists.php.net to get a copy of this message | ||
ID: 48315
Comment by: carsten_sttgt at gmx dot de
Reported By: alef0 at freenet dot de
Status: Open
Bug Type: Math related
Operating System: gentoo
PHP Version: 5.2CVS-2009-05-18 (CVS)
New Comment:
> $var = '122.34343The'; //expected float: 122.34343
> $float_value_of_var = floatval($var);
>
> Conversion using floatval() or (float) respects the
> numeric settings of the locale.
No, just make a:
| var_dump($float_value_of_var);
> print ' or floatval(' . $var . ') = ' . $float_value_of_var
."\n\n";
You are casting a float to a string, and that's where locale comes in
place. See [1]. And is this not the reason for setlocale: to output
dates/currencies/numbers/... according to a language setting?
If you want output the float value locale aware in a string, you can
use printf() with the "%F" modifier.
Regards,
Carsten
[1]
http://de.php.net/manual/en/language.types.string.php#language.types.string.casting
Previous Comments:
------------------------------------------------------------------------
[2009-05-18 15:41:31] alef0 at freenet dot de
At [27 Feb 2007 12:27pm UTC] tony2001@php.net wrote:
( ... ) we DON'T want scripts to depend on locale or we may end up
with
scripts working/not working with particular locales. (See Bug #40653)
------------------------------------------------------------------------
[2009-05-18 15:26:02] alef0 at freenet dot de
Description:
------------
PHP version: 5.2.6RC4-pl0-gentoo
Conversion using floatval() or (float) respects the numeric settings of
the locale.
THIS IS VERY DANGEROUS CAUSE YOU WILL GET WRONG RESULTS BY APPLYING ANY
MATH METHODE ON CONVERTED FLOATS!
Reproduce code:
---------------
$var = '122.34343The'; //expected float: 122.34343
setlocale(LC_NUMERIC, 'en_US.utf8');
$float_value_of_var = (float) $var;
print 'Locale dec sep point: (float) ' . $var . ' = ' .
$float_value_of_var;
$float_value_of_var = floatval($var);
print ' or floatval(' . $var . ') = ' . $float_value_of_var . "<br
/><br />\n\n";
setlocale(LC_NUMERIC, 'de_DE.utf8');
$float_value_of_var = (float) $var;
print 'Locale dec sep comma: (float) ' . $var . ' = ' .
$float_value_of_var;
$float_value_of_var = floatval($var);
print ' or floatval(' . $var . ') = ' . $float_value_of_var . "<br
/><br />\n\n";
Expected result:
----------------
Locale dec sep point: (float) 122.34343The = 122.34343 or
floatval(122.34343The) = 122.34343
Locale dec sep comma: (float) 122.34343The = 122.34343 or
floatval(122.34343The) = 122.34343
Actual result:
--------------
Locale dec sep point: (float) 122.34343The = 122.34343 or
floatval(122.34343The) = 122.34343
Locale dec sep comma: (float) 122.34343The = 122,34343 or
floatval(122.34343The) = 122,34343
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=48315&edit=1