|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-01-26 14:47 UTC] gregs at net-virtual dot com
Description:
------------
php -r '$a = 70687.465 + 8.930;$b = 70696.395;$c = 70687.464 + 8.936;printf("A: %s
%s %.9f\n", $a, money_format("%.2n", $a), $a);printf("B: %s %s %.9f\n", $b,
money_format("%.2n", $b), $b);printf("C: %s %s %.9f\n", $c, money_format("%.2n",
$c), $c);'
Output:
A: 70696.395 70696.39 70696.395000000
B: 70696.395 70696.40 70696.395000000
C: 70696.4 70696.40 70696.400000000
Why is A not getting rounded up to 70696.40?
Test script:
---------------
php -r '$a = 70687.465 + 8.930;$b = 70696.395;$c = 70687.464 + 8.936;printf("A: %s %s %.9f\n", $a, money_format("%.2n", $a), $a);printf("B: %s %s %.9f\n", $b, money_format("%.2n", $b), $b);printf("C: %s %s %.9f\n", $c, money_format("%.2n", $c), $c);'
Expected result:
----------------
A: 70696.395 70696.40 70696.395000000
B: 70696.395 70696.40 70696.395000000
C: 70696.4 70696.40 70696.400000000
Actual result:
--------------
A: 70696.395 70696.39 70696.395000000
B: 70696.395 70696.40 70696.395000000
C: 70696.4 70696.40 70696.400000000
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 17:00:02 2025 UTC |
Here is a easier to read version of the test code (I also added one more): $a = 70687.465 + 8.930; $b = 70696.395; $c = 70687.464 + 8.936; $d = 70687.464 + 8.931; printf("A: %s %s %.9f\n", $a, money_format("%.2n", $a), $a); printf("B: %s %s %.9f\n", $b, money_format("%.2n", $b), $b); printf("C: %s %s %.9f\n", $c, money_format("%.2n", $c), $c); printf("D: %s %s %.9f\n", $d, money_format("%.2n", $d), $d); Output: A: 70696.395 70696.39 70696.395000000 B: 70696.395 70696.40 70696.395000000 C: 70696.4 70696.40 70696.400000000 D: 70696.395 70696.40 70696.395000000This problem (if it is one) seems to extend to *printf* functions too: $a = 8.930 + 70687.465; $b = 70687.465 + 8.930; $c = 70696.395000; printf("A: %f, %.2f\n", $a, $a); printf("B: %f %.2f\n", $b, $b); printf("C: %f %.2f\n", $c , $c);' Output: A: 70696.395000, 70696.39 B: 70696.395000 70696.39 C: 70696.395000 70696.40 C version: #include <stdio.h> int main(void) { float a, b, c, d; a = 8.930 + 70687.465; b = 70687.465 + 8.930; c = 70696.395000; printf("A: %f %.2f\n", a, a); printf("B: %f %.2f\n", b, b); printf("C: %f %.2f\n", c, c); } Output: A: 70696.398438 70696.40 B: 70696.398438 70696.40 C: 70696.398438 70696.40With all due respect, you are not reading this. sprintf('%.2f', $float); number_format('%.2n', $float); should *ROUND* these numbers, the behavior is incorrectSorry, I meant money_format('%.2n', $float); In all of these cases the number should be rounded to 70696.40.Here is another example: php -r '$a = 70687.465 + 8.930;$b = 8.930 + 70687.465;$c = 70696.395000;printf("%f (%.2f) %f (%.2f) %f (%.2f)\n", $a, $a, $b, $b, $c, $c);'