Jump to content

Calculating / Rounding Up Problem


stig1

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/160556-calculating-rounding-up-problem/
Share on other sites

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 :)

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?

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.

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, '.', ',' );

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.

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.

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";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.