|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2009-02-04 12:41 UTC] lennart dot vandendool at fonq dot nl
 Description: ------------ Typecasting or converting some numeric strings variables to a float value does not work as expected. Reproduce code: --------------- $var = (float)"-39.000"; var_dump($var); $var = (float)"39.000"; var_dump($var); $var = (float)"17.000"; var_dump($var); $var = (float)"-17.000"; var_dump($var); $var = "-39.000" + 0; var_dump($var); $var = "39.000" + 0; var_dump($var); $var = "17.000" + 0; var_dump($var); $var = "-17.000" + 0; var_dump($var); Expected result: ---------------- float(-39) float(39) float(17) float(-17) float(-39) float(39) float(17) float(-17) Actual result: -------------- float(-38.:) float(38.:) float(16.:) float(-16.:) float(-38.:) float(38.:) float(16.:) float(-16.:) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 16:00:01 2025 UTC | 
The bug in Linux happens with echo 0.15-0.05;2010-06-14 09:30:45 BST I have reproduced this bug, but after changed some setting: change the "precision" and "serialize_precision" setting in php.ini, and even changed back, or maybe after i added the printf("%.2f\n",$v2), the echo $v2 is correctted, but the printf result is always 16.: finally, i wrote such a script: for ($i=0; $i<400; $i++) { echo $i."-"; printf("%f\n",$i); } and found there're 8 number have this bug, they are: 17 19 29 39 170 190 290 390 I'm using windows xp in a virtualbox virtual machine, and a delphi component named php4delphi, the php version is 5.2.13i tried set precision to 5/14/20, and keep the serialize_precision = 100. "echo number_format(1900.0,2)" is always wrong. here is my code: echo ini_get("precision")."/".ini_get("serialize_precision")."<br>"; echo number_format(1900.0,2)."-------\n";The same error occurred for me. The installation is PHP 5.2.13 installed on IIS7/Win server 2008. Writing a script as suggested (see below) produces incorrect output for the echo function. The error is however intermittent. When refreshing the browser and rerunning the script several times (perhaps 20-40 times) the correct numbers are sometimes displayed and sometimes the incorrect. Script used for testing: for ($i=0; $i<400; $i++) { echo $i."-"; printf("%f\n",$i); }Error occur also when fetching data from a MS SQL server having the column value 290.00 (database Money format). $query ="select pris from sa_jobb_3_1 WHERE id='".$id."'"; $result = mssql_query($query) or die($query); $row = mssql_fetch_array($result,MSSQL_ASSOC); printf("%f",$row['pris']); The code above outputs incorrectly 28:.000000 and not as expected 290.000000Similar Bug in PHP Version 5.3.5 My Code: ---------------------------------------------- $ADODB = new COM( 'ADODB.Connection' ); $ADODB->open($strConn); $RS = $ADODB->execute("SELECT Lagerwert FROM tblStammdaten WHERE tblStammdaten.Seriennummer='74925KR9';"); echo $RS->Fields(0)->Value . "<br>"; $x = sprintf("%f",$RS->Fields(0)->Value); echo $x; ---------------------------------------------- gives following result: ----------------------- 1900 18:0.000000 ----------------------------------------------- It only happends with the number or string 1900 and only if the execute command from the adodb connection was performed. I gues i also happens with other numbers.