Round calculation to the nearest pound

Hi guys,

I have the following calc, how can I round it to the nearest pound? £2.50 would be £3.00 and £2.49 would be £2.00 etc...

<?php $TheVar = $rows_product[$i]['product_rrp'] - $rows_product[$i]['product_price']; ?>

Who is Participating?
EmuL8_swConnect With a Mentor Commented:
Hi Dean,

You can use the normal round function integrated into php.
As described in the following documentation:

Just take note of the data type you pass to this function, as it does not like strings that are not formatted correctly.

I suppose in your case it would be something like:
$TheRRP = $rows_product[$i]['product_rrp'];
$ThePrice = $rows_product[$i]['product_price'];
$TheVar = $TheRRP - $ThePrice;
$TheVar = round($TheVar);

Open in new window

This piece of code is obviously very stretched out just for better reading.

There are two other functions as well:
ceil() - This one always rounds up no matter how small the decimal
floor() - This one always rounds down no matter how small the decimal.

Chris StanyonCommented:
If your variables contain the pound sign, then you'll need to strip that out. Also, if you need the output to include the decimals, you've got number_format().

$rrp = str_replace('£', '', $rows_product[$i]['product_rrp']);
$price = str_replace('£', '', $rows_product[$i]['product_rrp']);

$TheVar = round($rrp - $price);

echo number_format($TheVar,2);

Open in new window

SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi.. please use this

float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )


echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
Ray PaseurCommented:
Some good learning resources to help you get started with PHP...

... And some things to avoid as you get started with PHP...

Best of luck with your project, ~Ray
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.