stig1 Posted June 1, 2009 Share Posted June 1, 2009 The problem I am experiencing is numbers are rounding up when they do not require to be rounded up. For example 46.1700 would round up to 46.18, it should just stay at 46.17. Another example 37.7000 would round up to 37.71, it should just stay at 37.70. I have the following calculation: $uscos = 9.6000 $a = 4.0500; $aRate = number_format(($uscos * $a), 4, '.', ','); $aTotal = number_format((ceil(100 * $aRate) / 100), 2, '.',','); echo $aTotal; The above should return 46.17 not 46.18. How can I stop rounding up numbers, that do not require rounding up... Help is very appreciated. Quote Link to comment Share on other sites More sharing options...
stig1 Posted June 1, 2009 Author Share Posted June 1, 2009 The above will round up numbers that I require to be rounded up. For example 41.3820 will equal 41.39. It just struggles to not round up numbers that equal .1700 or 37.7000 basically numbers that do not require any rounding to happen. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 1, 2009 Share Posted June 1, 2009 I'm surprised it works at all since you have a syntax error and all. Quote Link to comment Share on other sites More sharing options...
stig1 Posted June 1, 2009 Author Share Posted June 1, 2009 The real version works as it does not have a syntax error. $uscos = 9.6000; $a = 4.0500; $aRate = number_format(($uscos * $a), 4, '.', ','); $aTotal = number_format((ceil(100 * $aRate) / 100), 2, '.',','); echo $aTotal; Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted June 1, 2009 Share Posted June 1, 2009 well, first of all I have no idea how you are getting 46.18 with the posted code because I am getting 38.89 I believe your issue is with ceil() since that is rounding up to do the calculation. Is there a particular reason you need that in there ? without ceil I am getting 38.88 which seems to be in line with what you are expecting ? plus you are missing a semicolon Quote Link to comment Share on other sites More sharing options...
stig1 Posted June 1, 2009 Author Share Posted June 1, 2009 Lonewolf217 you are correct, I should be seeing 38.88 as the answer, however i need to round numbers up that are like 38.882 to 38.89 hence why the ceil() is in the function, but it automatically rounds up numbers that come out as whole numbers making it not match with another system. How can I get ceil() to only round up numbers that do no equal 2 dec places exactly? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 1, 2009 Share Posted June 1, 2009 This is from my immediate window in my debugger: ceil( "3888" ) : double = 3888 100 * "38.88" : double = 3888 ceil( "3888" ) : double = 3888 ceil( "3888.00000" ) : double = 3888 100 * "38.88" : double = 3888 ceil( 100 * "38.88" ) : double = 3889 Very strange IMO. Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted June 1, 2009 Share Posted June 1, 2009 how about using round($num,2) to always round to 2 decimal places ? <?php $uscos = 9.6000; $a = 4.0500; $aRate = $uscos * $a; $aTotal = round((100*$aRate)/100,2); echo $aTotal; echos 38.88 Quote Link to comment Share on other sites More sharing options...
stig1 Posted June 1, 2009 Author Share Posted June 1, 2009 My function works for 95% of the calculations, just values that have .7000 that are whole in some way, round up to the next number. eg .7000 to .8000. or .8800 to .8900 for what is happening in the above example. Just want it to stay. Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted June 1, 2009 Share Posted June 1, 2009 <?php echo round(.7000,2); echo "<br>"; echo round(.8800,2); outputs .7 and .88 which is what you want right ? If you really need the trailing zero on .70 then you can use your number_format to clean it up again Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 1, 2009 Share Posted June 1, 2009 I believe PHP may be doing something funny because you are using a string in your arithmetic. I know my debugger showed a wrong calculation. Try this: $uscos = 9.6000; $a = 4.0500; $aRate = $uscos * $a; $aTotal = ceil(100 * $aRate) / 100; echo number_format( $aTotal, 2, '.', ',' ); Quote Link to comment Share on other sites More sharing options...
stig1 Posted June 1, 2009 Author Share Posted June 1, 2009 Lonewolf217 your calculation rounds down calculations that are: For example 38.8810 to 38.88 when i need that to go to 38.89. but when the calculation equals just 38.8800 it needs to stay and not round up. Quote Link to comment Share on other sites More sharing options...
stig1 Posted June 1, 2009 Author Share Posted June 1, 2009 roopurt18 your calculation rounds everything up. The calculation works out to be 38.88 but it will round up to 38.89, how can i stop ceil() rounding up numbers that already equal whole numbers with 2 dec places. So 38.8800 will stay as 38.88 but 38.8810 will go to 38.89. That's just the rule I need to enforce. Quote Link to comment Share on other sites More sharing options...
stig1 Posted June 1, 2009 Author Share Posted June 1, 2009 The other method i just thought about, not sure if it's possible. What if i do the calculation first, if the number is only 2 decimal places or less do nothing, just format into 2 decimal places, else if the number equals more than 2 decimal places run the ceil function? Would that work? If so, how do you find out how many decimal places in number so I can write the if statement. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 2, 2009 Share Posted June 2, 2009 Perhaps there's a better way, but: <?php $foo = 38.88; $foo = 38.880001; $places = 2; $mult = (int)pow( 10, $places ); if( ((double)$foo) === ( (double)(((int)(((double)$foo) * $mult)) / $mult) ) ) { echo 'no ceil!' . "\n"; }else{ echo 'ceil' . "\n"; } ?> 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.