[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

How can I round up and down in PHP?

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
Loganathan Natarajan
Asked:
Loganathan Natarajan
  • 6
  • 4
  • 2
  • +2
2 Solutions
 
rockiroadsCommented:
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
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
i tried that function , it does not give me the exact value i want it.. Can you give me example?
0
 
rockiroadsCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
rockiroadsCommented:
to round down, can u try this formula

floor(  (your number / 10) ) * 10

0
 
rockiroadsCommented:
and for rounding up try this


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

0
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
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
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
>> 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
 
rockiroadsCommented:
give me an example of what is not working. I tried what u posted up and it seemed fine.
0
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:

$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
 
rockiroadsCommented:
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
 
Marco GasiFreelancerCommented:
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
 
Rok-KraljCommented:
Can be done simpler:
$num = 4431.65;
$rnd = round($num/10)*10;
if ($num<$rnd) $rnd--;

echo $rnd;

Open in new window

0
 
ashokadiCommented:
<?
/*


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
 
Marco GasiFreelancerCommented:
Thanks for points! Bye
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now