php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48315 Type conversion into a float respects the numeric settings of the locale
Submitted: 2009-05-18 15:26 UTC Modified: 2009-05-18 18:29 UTC
From: alef0 at freenet dot de Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2CVS-2009-05-18 (CVS) OS: gentoo
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
3 + 26 = ?
Subscribe to this entry?

 
 [2009-05-18 15:26 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-18 15:41 UTC] 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 18:25 UTC] carsten_sttgt at gmx dot de
> $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
 [2009-05-18 18:29 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 08:02:42 2024 UTC