Solved

How can I round up and down in PHP?

Posted on 2010-09-20
14
444 Views
Last Modified: 2013-11-13
for example if i have values like these, and i need to round it,

if i have value,  2134.21 ==> Round up to 2130
if i have value,  4435.65 ==> Round up to 4439

if i have value 385 ==> Round up to 389
if i have value 382 ==> Round up to 380

How can I do it?
0
Comment
Question by:Loganathan Natarajan
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33722252
have a look at floor and ceil functions

ceil - round up
floor - round down

or maybe specify the round type as described here http://php.net/manual/en/function.round.php
0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 33722304
i tried that function , it does not give me the exact value i want it.. Can you give me example?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33722340
Actually just looked again closely at your sample data. When u round up you want it to the next 9? and rounded down to 0?



0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 65

Expert Comment

by:rockiroads
ID: 33722357
to round down, can u try this formula

floor(  (your number / 10) ) * 10

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33722361
and for rounding up try this


 (  ceil(  (your number / 10) ) * 10 ) - 1

0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 33722496
both are not returning what i expect the values,

echo "value 1 - ".floor( ($num / 10) ) * 10

echo "value 2".ceil(($num / 10) ) * 10 ) - 1
0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 33722498
>> Actually just looked again closely at your sample data. When u round up you want it to the next 9? and rounded down to 0?


correct...

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33722551
give me an example of what is not working. I tried what u posted up and it seemed fine.
0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 33722568

$num = 4435.65;

echo "value 1 - ".floor( ($num / 10) ) * 10

echo "value 2".ceil(($num / 10) ) * 10 ) - 1

---------

i expect values $num = 4439, 4430
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 200 total points
ID: 33722573
2134.21 ==> 2130   running this floor((2134.21 / 10) ) * 10  produces 2130

4435.65 ==> 4439   running this  (ceil((4435.65 / 10) ) * 10 ) - 1 produces 4439


385 ==> 389    running this  (ceil((385 / 10) ) * 10 ) - 1 produces 389
382 ==> 380    running this floor((382 / 10) ) * 10 produces 380

0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 50 total points
ID: 33722578
Try code attached: it does the trick.

Cheers.
<?php
/*
if i have value,  2134.21 ==> Round up to 2130
if i have value,  4435.65 ==> Round up to 4439

if i have value 385 ==> Round up to 389
if i have value 382 ==> Round up to 380
*/
function myround($n){
    $temp = explode(".", $n);
    $intnum = $temp[0];
    $lastnum = substr($intnum, strlen($intnum)-1, 1);
    if ($lastnum < 5){
        $lastnum = 0;
    }else{
        $lastnum = 9;
    }
//    return $lastnum;
    return str_replace(substr($intnum, strlen($intnum)-1, 1), $lastnum, $intnum);
}

$n1 = 2134.21;
$n2 = 4435.65;
$n3 = 385;
$n4 = 382;


echo myround($n1);
echo "<br />";
echo myround($n2);
echo "<br />";
echo myround($n3);
echo "<br />";
echo myround($n4);
echo "<br />";
?>

Open in new window

0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33722728
Can be done simpler:
$num = 4431.65;
$rnd = round($num/10)*10;
if ($num<$rnd) $rnd--;

echo $rnd;

Open in new window

0
 
LVL 1

Expert Comment

by:ashokadi
ID: 33728011
<?
/*


try this you can get the solution
2134.21;

4435.65;

385 ;
*/

$numa = 4435.65 ;
printf(" <br> %d <br>",($numa%10)?((($numa%10)<5)?(10*(int)($numa/10)):((10*(int)($numa/10))+9)):($numa));

?>
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33760307
Thanks for points! Bye
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

809 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