|  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
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
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-06-26 12:27 UTC] rasmus at mindplay dot dk
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:

Thank You.

Reproduce code:

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;
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:

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


Add a Patch

Pull Requests

Add a Pull Request


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

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: Mon Apr 15 00:01:28 2024 UTC