#41065 [Opn->Bgs]: number_format() inconsistant rounding

From: Date: Thu, 12 Apr 2007 16:26:05 +0000
Subject: #41065 [Opn->Bgs]: number_format() inconsistant rounding
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-111416@lists.php.net to get a copy of this message
ID: 41065 Updated by: tony2001@php.net Reported By: michel dot gervais at hrsdc-rhdsc dot gc dot ca -Status: Open +Status: Bogus Bug Type: *Math Functions Operating System: win2k PHP Version: 4.4.6 New Comment: Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations. If you would like to know more about "floats" and what IEEE 754 is read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. Previous Comments: ------------------------------------------------------------------------ [2007-04-12 16:15:58] michel dot gervais at hrsdc-rhdsc dot gc dot ca Description: ------------ number_format(2009.00 * 0.2550,2); = 512.29 <= NOT rounded ---------------- Similar to Bug #22712 << whch has been marked as "bogus/closed" has actuall errors, as last poster mentions. <? echo number_format(63.745, 2, '.', ""); echo number_format(64.745, 2, '.', ""); ?> Result: 63.74 64.75 --------------- Reproduce code: --------------- number_format(2009.00 * 0.2550,2); = 512.29 << NOT rounded Should be 519.30 The following 2 lines "are" rounded. number_format(5949.00 * 0.2550,2); = 1,517.00 << is rounded number_format(721.00 * 0.2550,2); = 183.86 << is rounded for some reason it doesn't round 512.29 to 512.30 but it works fine for 1,517.00 => 1,516.99 183.86 => 183.855 ------------------------------------------------------------ Code <? echo number_format(5949.00 * 0.2550,2); ?> <? echo number_format(2009.00 * 0.2550,2); ?> <? echo number_format(721.00 * 0.2550,2); ?> Expected result: ---------------- 1,517.00 512.30 183.86 Actual result: -------------- 1,517.00 512.29 183.86 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=41065&edit=1

« previous php.bugs (#111416) next »