php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41246 Setting locales to german and applying utf8_encode to a float messes it up.
Submitted: 2007-05-01 00:00 UTC Modified: 2007-05-01 15:25 UTC
From: phil at goli dot at Assigned:
Status: Not a bug Package: ICONV related
PHP Version: 5.2.1 OS: Linux Gentoo
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phil at goli dot at
New email:
PHP Version: OS:

 

 [2007-05-01 00:00 UTC] phil at goli dot at
Description:
------------
Applies to PHP versions 4.4.1, 4.4.4, 5.2.1, etc.

1. setlocales to german:
   setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');

2. assign a float to a variable by defining a string: $myTest = '10.01'; 

3. add a float to that string: $myTest = $myTest + 0.01;

4. convert that float to utf8: 
   $myTest = utf8_encode($myTest);  

5. add another float to that utf8 string: $myTest = $myTest + 0.01;

6. if the german locales are installed on the system then the output
   will be wrong. utf8_encode chops off the positions after decimal
   point.



Reproduce code:
---------------
    $loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
    echo '<html><body><form><textarea cols="40" rows="40">';
    echo 'Preferred locale for german on this system is "' . $loc_de . '"' . "\n";
    echo 'Example only works if locales for german are installed!' . "\n\n";
    echo 'Normal:' . "\n";
    $myTest = '10.01';  var_dump($myTest);
    $myTest = $myTest + 0.01;  var_dump($myTest);
    // no utf8_encode
    $myTest = $myTest + 0.01;
    echo 'Result should be 10.03 and it is: ';
    var_dump($myTest);
    echo "\n\n" . 'With utf8_encode:' . "\n";
    $myTest = '10.01';  var_dump($myTest);
    $myTest = $myTest + 0.01;  var_dump($myTest);
    $myTest = utf8_encode($myTest);  var_dump($myTest);
    $myTest = $myTest + 0.01;
    echo 'Result should be 10.03 BUT it is: ';  var_dump($myTest);
    echo '</textarea></form></body></html>';
    exit;

Expected result:
----------------
Preferred locale for german on this system is "de_DE@euro"
Example only works if locales for german are installed!

Normal:
string(5) "10.01"
float(10,02)
Result should be 10.03 and it is: float(10,03)


With utf8_encode:
string(5) "10.01"
float(10,02)
string(5) "10,02"
Result should be 10.03 ... and it is: float(10,03)



Actual result:
--------------
Preferred locale for german on this system is "de_DE@euro"
Example only works if locales for german are installed!

Normal:
string(5) "10.01"
float(10,02)
Result should be 10.03 and it is: float(10,03)


With utf8_encode:
string(5) "10.01"
float(10,02)
string(5) "10,02"
Result should be 10.03 ... and it is: float(10,01)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-01 13:19 UTC] mike@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

mike@honeybadger:~$ php -r 'echo 10.01,"\n";'
10.01
mike@honeybadger:~$ php -r 'setlocale(LC_ALL,"de_DE"); echo 10.01,"\n";'
10,01

 [2007-05-01 14:20 UTC] phil at goli dot at
sorry for bothering but i'm quite sure there's something wrong. do you have locales installed on your machine? your code 

php -r 'setlocale(LC_ALL,"de_DE"); echo 10.01,"\n";'

doesn't use utf8_encode which causes the problem together (and ONLY if locale de_DE is really installed on your system) with setlocale(LC_ALL,"de_DE"). so it doesn't prove that this is not a bug.

please try these two lines:

$loc_de = setlocale(LC_ALL,"de_DE");
echo 'Example only works if locale de_DE is installed! Current locale: "' . $loc_de . '". Output: ' .  (utf8_encode(0.01) + 0.01) . "<br>";

Summary: a string that comes out of utf8_encode is not automatically type casted to float when adding another float to it (this would normally happen if you don't us utf8_encode). but this whole scenario is only the case if setlocale(LC_ALL,"de_DE") is used!
 [2007-05-01 14:48 UTC] mike@php.net
Did you actually read my command lines?

It shows that PHP automatically converts floats to the locale's display format when they're converted to strings.  It has nothing to to with utf8_convert() and is expected bahaviour.

<quote url="http://de2.php.net/manual/en/language.types.string.php#language.types.string.casting">
An integer or a floating point number (float) is converted to a string representing the number with its digits (including the exponent part for floating point numbers).

Note: The decimal point character is defined in the script's locale (category LC_NUMERIC). See setlocale(). 
</quote>
 [2007-05-01 15:25 UTC] phil at goli dot at
i see your point but in my opinion string conversion to numbers is not 100% correct.

http://de2.php.net/manual/en/language.types.string.php#language.types.string.conversion
>String conversion to numbers
>When a string is evaluated as a numeric value, the resulting value and >type are determined as follows.
>The string will evaluate as a float if it contains any of the >characters '.', 'e', or 'E'. Otherwise, it will evaluate as an integer. 

the PHP internal automatic type cast from a string to a float should not look for the character '.' in a string but should look for the decimal point character that is defined in the script's locale. that would be consistent. right?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 17:01:29 2024 UTC