|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-11-23 10:50 UTC] cyrille dot php at giquello dot fr
Description: ------------ Hi, I've read the warning about float on documentation http://php.net/manual/fr/language.types.float.php but for simple computing it should be nice if Php return value like other languages (Python & Java). The computing (int)( 103.24 * 6 * 10**2) should return 61944 nor 61943. I've done the same with Java & Python (converting float result to integer) and get the right result: 61944 Test script: --------------- php -r 'echo (int)( 103.24 * 6 * 10**2),"\n";' Expected result: ---------------- 61944 Actual result: -------------- 61943 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 05:00:01 2025 UTC |
I have no idea what you tried in Python and Java, but it was clearly something else, because these are standard floating point semantics that are consistent across all languages. I've just checked `(int)(103.24 * 6 * 10**2)` on Python 3.6 and it indeed gives 61943, as expected. If you don't want truncating conversion ("round down" for positive numbers), don't use integer casts, or ensure that the floating point number represents an exact integer beforehand, by using round().In Java float a = (float) (103.24 * 6 * Math.pow(10,2)) ; System.out.println( a ); // print 61944.0 System.out.println( (int) a ); // print 61944, not 61943