Solved

Round calculation to the nearest pound

Posted on 2013-11-19
4
1,006 Views
Last Modified: 2013-11-25
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
Comment
Question by:deanlee17
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 4

Accepted Solution

by:
EmuL8_sw earned 500 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);
?>

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.

Regards
SW
0
 
LVL 43

Expert Comment

by:Chris Stanyon
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);

Open in new window

0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39659102
hi.. please use this

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 110

Expert Comment

by:Ray Paseur
ID: 39659346
Some good learning resources to help you get started with PHP...
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question