Edit report at https://bugs.php.net/bug.php?id=61787&edit=1
ID: 61787 Updated by: ras...@php.net Reported by: ubuntu at spacetrace dot org Summary: money_format round error Status: Not a bug Type: Bug Package: Math related Operating System: Linux ubuntu 11.04 PHP Version: 5.3.10 Block user comment: N Private report: N New Comment: No, that won't work. Often the value comes from expressions that always generate lots of digits. eg. number_format('%.2i', 1/3) What would you expect that to do? You wold get a notice every single time even though there may not be any relevant loss of precision. There are just certain things you need to eventually learn when you start programming. Previous Comments: ------------------------------------------------------------------------ [2012-04-22 22:45:48] ubuntu at spacetrace dot org maybe an efficient way would be to see if the desired amount of digits is less than the amount of digits in the float. in my example where i tried to show 1.005 with only 2 fractional digits, the notice could be like: "Error: input float exceeds maximum number of fractional digits" this would save thousands of hours of debugging worldwide. i am so keen on this issue, cause it cost me already about 100 hours of work to find out what was the cause, why in my project my bills every now and then where not correctly rounded. it is a stupid bug really hard to track and i am sure am not the only one that had this problem. if php would throw a notice, that would save lots of hazzle for not-so-experiensed programmers. how can you know, that you shouldn't use floats in money_format. this is a kind of secret knowledge right now especially cause the manual says: "string money_format ( string $format , float $number )" ------------------------------------------------------------------------ [2012-04-22 20:40:22] ras...@php.net There is no efficient way to "notice the error" If you want slow and accurate floating point manipulation it is available via the arbitrary precision extensions like bcmath and gmp. ------------------------------------------------------------------------ [2012-04-22 20:21:49] ubuntu at spacetrace dot org i am sorry, i didnt want to do any harm. i perfectly understood it before already. But what do you think about my suggestions? 1. a notice error or 2. round it like the round() function ------------------------------------------------------------------------ [2012-04-21 22:25:45] ras...@php.net This is getting a bit tiresome. There is no bug here. Maybe you will understand it this way. Try running this: <?php ini_set('precision',32); $ns = array(1.005, 7.005, 8.005); foreach($ns as $n) { echo "$n ".money_format('%.2i', $n)."\n"; } The output on my machine is: 1.004999999999999893418589635985 1.00 7.004999999999999893418589635985 7.00 8.0050000000000007815970093361102 8.01 That is, 1.005 can't actually be represented accurately and it ends up being slightly below 1.005 which means when you round it you get 1.00. And 8.005 ends up being represented as slightly larger than 8.005 so when you round it you get 8.01. It makes perfect sense. Simply add a fuzz factor to your floating point values to the appropriate precision you care about. Or, as most people know, when dealing with money, don't use floating point at all. Work in integers. ------------------------------------------------------------------------ [2012-04-21 21:55:42] ubuntu at spacetrace dot org (i meant: money_format('%.2i',8.005) results in 8.01) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=61787 -- Edit this bug report at https://bugs.php.net/bug.php?id=61787&edit=1