php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45365 non-native conversions from float to string
Submitted: 2008-06-26 12:27 UTC Modified: 2015-01-04 04:22 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: rasmus at mindplay dot dk Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.2.6 OS: *
Private report: No CVE-ID: None
 [2008-06-26 12:27 UTC] rasmus at mindplay dot dk
Description:
------------
When using strval(), implicit conversion or type-casting, floats are converted using locale-aware display formatting, rather than native formatting, as understood by most other software (and PHP itself).

Please understand that this is a complex problem, which requires a lengthy analysis and many examples. I have spent almost 6 hours analyzing this problem in-depth, and created a complete report explaining the problem, suggesting changes, and recommending ways to implement these changes.

Please, don't discard this report without careful consideration - there are fundamental problems with float-handling in PHP, which should be taken seriously.

For the full description and examples, please see this page:

http://public.test.fynskemedier.dk/float/

Thank You.


Reproduce code:
---------------
<?php

setlocale(LC_NUMERIC, 'danish');

function ftos($f) {
    return is_float($f) ? sprintf('%.14F', $f) : 'NaN';
}

$a = 1234.01234567890123456789;

echo "accurate value: " . ftos($a) . "\n\n";
echo "casting float to string: ";
$s = (string) $a;
var_dump($s);
echo "casting back to float: ";
echo ftos( (float) $s ) . "\n\n";

?>

The above is a very simple example, demonstrating one of the issues most commonly encountered by beginning PHP developers.

Please find many more examples and in-depth explanations below:

http://public.test.fynskemedier.dk/float/


Expected result:
----------------
accurate value: 1234.01234567890128

casting float to string: string(19) "1234.01234567890128"
casting back to float: 1234.01234567890128


Actual result:
--------------
accurate value: 1234.01234567890128

casting float to string: string(13) "1234,01234568"
casting back to float: 1234.00000000000000


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-08 21:53 UTC] jani@php.net
-Package: Feature/Change Request +Package: Math related
 [2011-04-08 21:53 UTC] jani@php.net
-Package: Math related +Package: Scripting Engine problem
 [2014-12-27 20:09 UTC] kalle@php.net
-Status: Open +Status: Feedback
 [2014-12-27 20:09 UTC] kalle@php.net
Please try using this snapshot:

  http://snaps.php.net/php-trunk-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/

Float handling had a major revamp in 5.3, can you please try a snapshot and check if this bug still is present?
 [2015-01-04 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 11 09:01:28 2024 UTC