Rounding up to the nearest 1

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

Rounding up to the nearest 1

Post by larrytech »

Hi,

I am working on a postal price calculator and it has all been well and good so far. However. when the weight exceeds 1000g, it costs 89p per 250g or part thereof after.

Code: Select all

<?php
if($postageweight>=1000 AND $deliverytype==rd){
$excessweight = $postageweight-1000;
$excesschargeportions = $excessweight/250;
$excesscharge = $excesschargeportions * 0.89;
$rawpostageprice = 4.31 + $excesscharge;
$postageprice = sprintf("%0.2f",$rawpostageprice);
}
?>
So far I have this, and this is good because it works out a fractioln of the excess charge and adds it. However, if the $excesschargeportions is anwhere between 0 and 0.999... for instance I need it to round to 1 so the whole 89p will be added.

Does anybody know how this can be done?

Many thanks,
Lawrence
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

Try:

Ref: http://uk2.php.net/round

Regards,
larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

Post by larrytech »

Thanks for the link. However, I have read through all of this advice and nowhere does it cover rounding up like this. As far as i can see it covers rounding to the nearest something...

i.e.
4.5 to 5
4.6 to 5
4.4 to 4

The thing is I need it to round 0.1 to 1 rather than 0. If I am missing something here please tell me :). I cannot think of the degree of accuracy I need to specify to get it to do this because however you look at it 0.1 is close to 0 than 1.

Thanks,
Lawrence
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

float ceil(float value)

ex:
$num=ceil(10.002);
echo $num; // prints 10
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Hmm, don't you mean that it will print 11?? ceil will print the lowest integer greater than or equal to its input. floor will do the greatest integer less than or equal to its input
larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

Post by larrytech »

Wow! Thank you so much everybody. ceil is working very well as follows

Code: Select all

<?php
if($postageweight>=1000 AND $deliverytype==rd){
$excessweight = $postageweight-1000;
$excesschargeportions = $excessweight/250;
$excesschargeportionsrounded = ceil($excesschargeportions);
$excesscharge = $excesschargeportionsrounded * 0.89;
$rawpostageprice = 4.31 + $excesscharge;
$postageprice = sprintf("%0.2f",$rawpostageprice);





echo "Excess Weight: $excessweight<br>";
echo "How many lots of 250g?: $excesschargeportionsrounded<br>";
echo "How much of 89p?: $excesscharge<br>";
echo "Total Postage: $rawpostageprice<br>";
echo "Pretty Postage: $postageprice<br>";

}
?>
I have tested this and it works splendidly.

Thanks,
Lawrence
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

daven wrote:float ceil(float value)

ex:
$num=ceil(10.002);
echo $num; // prints 10
Actually it would come out to be 11. ;) You're thinking of floor();
Post Reply