-5

I want to round up the values in PHP. please suggest me any function how to implement the values in round.

example how to convert it round as below:

803   --> 800 

791.5 --> 790

811   --> 810

845.7 --> 850

850.6 --> 860

900.5 --> 900

1016.9 --> 1020
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Have you searched on google ? – Rizier123 Apr 07 '15 at 11:27
  • You're not "rounding up", you're [rounding](http://cstl.syr.edu/fipse/decunit/roundec/roundec.htm).... except for `850.6 --> 860` which (surely) should become `850`..... and the answer is [here](http://www.php.net/manual/en/function.round.php] – Mark Baker Apr 07 '15 at 11:29
  • 1
    What is rounding up here `803 --> 800` and `811 --> 810` ? – Keep Coding Apr 07 '15 at 11:29
  • Your "rounding" is not consistent. It is unclear what you are asking for because you don't describe clearly how your desired results are obtained. – TZHX Apr 07 '15 at 11:33
  • 1
    This one doesn't match the others 850.6 --> 860 as far as pattern goes. – Funk Forty Niner Apr 07 '15 at 11:34
  • What is the logic in: `850.6 --> 860` `900.5 --> 900`? These make no sense whatsoever compared to eachother. – Loko Apr 07 '15 at 11:42
  • Please be clear in your rounding pattern, It doesn't makes any sense as the above user's pointed out. – Sulthan Allaudeen Apr 07 '15 at 11:48

3 Answers3

0

for 803->800 use round(803,-1), for 900.5->900 use floor(900.5).

for more examples you better take a look at http://php.net/manual/en/function.round.php

Itamar
  • 157
  • 9
0

Since there is nothing in common with the examples, I'll do them 1 by 1 for you:

echo floor(803/100)*100; --> 800

echo floor(791.5/10)*10; --> 790

echo floor(811/10)*10; --> 810

echo ceil(845.7/10)*10; --> 850

echo ceil(850.6/10)*10; --> 860

echo floor(900.5/100)*100; --> 900 // echo floor(900.5/10)*10 works as well

echo ceil(1016.9/10)*10; --> 1020

Just in case you dont get it: You make the number smaller, to get the floor/ceil function to get the proper result divided by 10 after using the floor/ceil function, you multiply by 10.

Loko
  • 6,539
  • 14
  • 50
  • 78
0

If you want to do it mathematically, This is how you can do a follow...

845.7 % 10 = 5.7(reminder)

let, you want to round up 900.5;

<?php
$value = round(900.5);
$roundedValue = 0;

if(($value % 10) > 5){ 
        $roundedValue = intval($value / 10) * 10 + 10 ; 
     }else{
         $roundedValue = intval($value / 10) * 10;
    }
    echo "The value is: ".$roundedValue;
?>

Then $roundedValue is what you want! Hope this help. Best of luck.

Imran
  • 4,582
  • 2
  • 18
  • 37