dyedblue Posted December 18, 2007 Share Posted December 18, 2007 I have a money vouchering form that users input an invoice amount and select percentages (up to three) that the form calculates the amount from each fund for. It works most of the time but I am being thrown off because of a rounding issue that causes the total of the calculated amounts for the 3 funds not to equal the invoice amount. It is off by $.01. I have the following code and have tried several approaches the latest being to take the sum of the 2nd and 3rd amounts and subtract that from the invoice amount to give me the 1st amount. The 1st amount is still often off by $.01. Is there anyone who has experienced a similar problem that could shed some light on the subject for me or that has an easier approach to my problem? //Calculates the Amounts to be charged to each fund based on percentages entered $percent = 100; //Calculates the second fund amount $percentagecalc2=$percentage2 / $percent; $amtr2=$amt * $percentagecalc2; $amt2 = preg_replace('/([\d,]+.\d{2})\d+/', '$1', $amtr2); $amt2 = number_format($amt2, 2, '.', ','); //Calculates the third fund amount $percentagecalc3=$percentage3 / $percent; $amtr3=$amt * $percentagecalc3; $amt3 = preg_replace('/([\d,]+.\d{2})\d+/', '$1', $amtr3); $amt3 = number_format($amt3, 2, '.', ','); //Calculates the first fund adds penny if needed $amtr1=$amt-($amtr2+$amtr3); $amt1 = number_format($amtr1, 2, '.', ','); Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 18, 2007 Share Posted December 18, 2007 Could you also post the numbers that you are using, the expected results and the actual results? Quote Link to comment Share on other sites More sharing options...
dyedblue Posted December 18, 2007 Author Share Posted December 18, 2007 *Values passed in from form $amt=279.71 $percentage2=40 $percentage3=20 $amt2=111.88 $amt3=55.94 When these numbers are entered I get $amt1=111.88 and it should be $111.89 (279.71 - 167.82) Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 18, 2007 Share Posted December 18, 2007 You are rounding before the end of the calculation, which gives the the wrong expected result, the actual result is correct. When you say (279.71 -167.82) = 111.89, that is true, however, the program won't round until the end and it doesn't get 167.82, it gets 167.826, which changes the answer to 111.884, and that rounds down to .88. Quote Link to comment Share on other sites More sharing options...
dyedblue Posted December 19, 2007 Author Share Posted December 19, 2007 I understand what you are saying, but I don't see where my error is. Quote Link to comment Share on other sites More sharing options...
richardw Posted December 19, 2007 Share Posted December 19, 2007 As lemmin said, round at the end only. Remove your number_format from all but the variable set to display the results. Best Quote Link to comment Share on other sites More sharing options...
dyedblue Posted December 19, 2007 Author Share Posted December 19, 2007 In my code ($amtr1=$amt-($amtr2+$amtr3); I use the pre-rounded numbers ($amtr2, $amtr3) to get the first amount. I am using $amt1, $amt2, $amt3 to display the amounts so I don't want to take off the rounding for them, but as far as I can tell I only use the pre-rounding amounts to do the calculations. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 19, 2007 Share Posted December 19, 2007 To get the most accurate results, you would want to round at the end.... Example: $num1 = 5.5; $num2 = 5.5; $preround = round($num1) + round($num2); echo $preround; //12 echo round($num1 + $num2);11 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.