|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-09-15 14:06 UTC] sniper@php.net
[2004-09-15 22:59 UTC] iliaa@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 06:00:01 2025 UTC |
Description: ------------ After exploding a string into its components which contain type float, subsequent multiplication and rounding with those strings doesn't work unless specifically casting them as type float - ie. (float)$strarray[0]. This particularly seems to be the case if the result is < 1. Where the result is > 1, the code below seems to work. This appears to be a recent change in the behavior of php, since previous versions automatically and correctly interpreted strings as floats where necessary for related math operations (such as multiplication and rounding). This may be my own misconception about how things should work, but I am reporting it in the event that there has been some problem with the string type handling introduced in recent versions of php. If not, sorry for wasting your time and I'd appreciate an explanation of why our code has broken. Thanks. Reproduce code: --------------- failing code: $astr = "2|.9456|.0066778|17" $numarray = explode("|",$astr); $result = round($numarray[0] * $numarray[2],4); echo $result; working code: $astr = "2|.9456|.0066778|17" $numarray = explode("|",$astr); $result = round((float)$numarray[0] * (float)$numarray[2],4); echo $result; (I have had to apply this workaround to code which previously worked with earlier versions of php without the additional type-casting.) Expected result: ---------------- .0134 Actual result: -------------- 0