Page 1 of 1

Rounding up to the nearest 1

Posted: Wed Jul 02, 2003 1:55 pm
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

Posted: Wed Jul 02, 2003 1:57 pm
by cactus
Try:

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

Regards,

Posted: Wed Jul 02, 2003 2:00 pm
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

Posted: Wed Jul 02, 2003 2:10 pm
by daven
float ceil(float value)

ex:
$num=ceil(10.002);
echo $num; // prints 10

Posted: Wed Jul 02, 2003 2:13 pm
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

Posted: Wed Jul 02, 2003 2:20 pm
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

Posted: Wed Jul 02, 2003 4:55 pm
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();