Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Round calculation to the nearest pound

Posted on 2013-11-19
Medium Priority
1,341 Views
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']; ?>

Thanks,
Dean
0
Question by:deanlee17

LVL 4

Accepted Solution

EmuL8_sw earned 2000 total points
ID: 39658835
Hi Dean,

You can use the normal round function integrated into php.
As described in the following documentation:
http://php.net/manual/en/function.round.php

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:
``````<?php
\$TheRRP = \$rows_product[\$i]['product_rrp'];
\$ThePrice = \$rows_product[\$i]['product_price'];
\$TheVar = \$TheRRP - \$ThePrice;
\$TheVar = round(\$TheVar);
?>
``````

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.

Regards
SW
0

LVL 45

Expert Comment

ID: 39659066
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);
``````
0

LVL 11

Expert Comment

ID: 39659102

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

or

<?php
echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
echo(round(-4.60));
?>
0

LVL 111

Expert Comment

ID: 39659346
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

... And some things to avoid as you get started with PHP...
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12293-AntiPHPatterns-and-AntiPHPractices.html

Best of luck with your project, ~Ray
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called \$myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
###### Suggested Courses
Course of the Month15 days, 9 hours left to enroll