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
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
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 :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.