1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

round number to nearest half

Discussion in 'PHP' started by jacka, Jul 21, 2008.

  1. #1
    Hi all

    Could you please guide me to how to round a decimal number to nearest half using php.
    eg. 2.213 => 2.5
    2.76 => 3.0
    3.03 => 3.5

    With thanks.
    :confused::confused:
     
    jacka, Jul 21, 2008 IP
  2. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    
    <?php
    	$input = '2.35';
    	$yournum = round(($input*2), 0)/2;
    	echo $yournum;
    ?>
    
    
    PHP:
    best i can do
     
    cornetofreak, Jul 21, 2008 IP
  3. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Thx for your reply.
    It is not quite right
    for example: input=15.56, the answer should be 16.00.
    where as 25.56*2 =31.12
    round (31.12)=31
    31/2=15 incorrect.

    Just to put it in correct wording, I wish to ROUND UP to nearest half value.
    thx
     
    jacka, Jul 21, 2008 IP
  4. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Job done.
    As the numbers were stored in database, I compared all the prices that was above the requested number and sorted by Ascending order and chose the first array.
     
    jacka, Jul 21, 2008 IP
  5. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #5
    $input = '15.56 ';
    $output = round(round(($input*4))/4);
    echo $output;
     
    php-lover, Jul 21, 2008 IP
  6. ad network

    ad network Peon

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    to make sure it always works :

    $input = '15.56 ';
    $output = round($input*10)/10;
    echo $output;


    plus you save a round function.
     
    ad network, Jul 21, 2008 IP
  7. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You should actually create your own function for this as PHP's round function behaves differently than what you want.
    PHP's round function will result in the following:

    
    echo round(1.4);   // 1
    echo round(1.5);   // 2
    
    PHP:
    The following code will give you what you want:

    function roundUp($value, $decimalPlaces)
    {
        return ceil($value * pow(10, $decimalPlaces)) /  pow(10, $decimalPlaces);
    }
    
    function roundDown($value, $decimalPlaces)
    {
        return floor($value * pow(10, $decimalPlaces)) / pow(10, $decimalPlaces);
    }
    
    PHP:
     
    ahowell, Jul 21, 2008 IP
  8. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #8
    function round_to_half($num)
    {
      if($num >= ($half = ($ceil = ceil($num))- 0.5) + 0.25) return $ceil;
      else if($num < $half - 0.25) return floor($num);
      else return $half;
    }
    PHP:
    Test:

    1 - 1
    1.05 - 1
    1.1 - 1
    1.15 - 1
    1.2 - 1
    1.25 - 1.5
    1.3 - 1.5
    1.35 - 1.5
    1.4 - 1.5
    1.45 - 1.5
    1.5 - 1.5
    1.55 - 1.5
    1.6 - 1.5
    1.65 - 1.5
    1.7 - 1.5
    1.75 - 2
    1.8 - 2
    1.85 - 2
    1.9 - 2
    1.95 - 2
    2 - 2
    
    Code (markup):
    Doesn't work for negatives, tell me if you need this too and I'll add it.
     
    Danltn, Jul 21, 2008 IP
    yourwish likes this.
  9. torkil

    torkil Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Alot of complex solutions here!

    To break up the problem a bit more:

    What you want is
    - a number that can be divided into 0.5s
    - you want it to be bigger than, or equal to, the number you have.

    So 2.083 should become 2.5, 2.51 should become 3, and so on.

    What you do is you divide the number into 0.5s. 2.083/0.5 = 4.116. To get a number that can be divided into 0.5, we just round upwards with ceil(). ceil(4.116) = 5. Then we multiply again... 5*0.5 = 2.5.

    Here is the code:

    
    $number   = 15.20; // Replace with the number you wish to round
    $rounding = 0.5;   // Replace this with whatever you want to round to
    
    $roundedUp   = ceil($number/$rounding)*$rounding;  // = 15.5
    $roundedDown = floor($number/$rounding)*$rounding; // = 15
    $rounded     = round($number/$rounding)*$rounding; // = 15
    
    Code (markup):
    This code is very general, so by replacing the number and rounding you can use it to round to pretty much anything. It works with negative numbers too.
     
    torkil, Aug 10, 2009 IP
  10. wizzuriz

    wizzuriz Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hello all.

    I'm new to programming but, i'm working hard on getting better. I have a question about round, I did check the forum and this function might be some thing i can use however, it might need a little work.

    function roundUp($value, $decimalPlaces)
    {
    return ceil($value * pow(10, $decimalPlaces)) / pow(10, $decimalPlaces);
    }
    
    function roundDown($value, $decimalPlaces)
    {
    return floor($value * pow(10, $decimalPlaces)) / pow(10, $decimalPlaces);
    PHP:

    The thing is I have 90 different values, from 30 to 140 and I want to round them off, please see example.

    result : 41,11 should be 40.00 and 41,31 should 42.50. ( I only want to use xx.00 & 2.50 & 5.00 & 7.50 or xx.00 ) please let me know if there is any way to make a nice function that will help me round my results.

    Best regards
    Wizzuriz
     
    wizzuriz, Aug 22, 2011 IP
  11. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #11
    If you tell us what the logic is that rounds 41.11 to 40.00 but rounds 41.31 to 42.50. (US, so . not , but the meaning is the same.) If you;re rounding to the nearest half, 41.31 is 41.50, not 42.50. Are you rounding or rounding up? (Rounding rounds to the nearest X, whether that's up or down.)
     
    Rukbat, Aug 22, 2011 IP
  12. wizzuriz

    wizzuriz Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    You see the thing is I need some values that only goes in 2.5 row. 40.00 - 42.50 - 45.00 - 47.5 - 50.00

    2.5/2 = 1.125 ( 1.25> round to 40.00, 126< round to 42.50 )

    If you have a function where I can round off like that, please do let me know.

    Best regards
    wizzuriz.
     
    wizzuriz, Aug 23, 2011 IP
  13. wizzuriz

    wizzuriz Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hello all.


    I'm new in programming and I have a problem with rounding some values.


    how could this work?


    function round($my_number){

    I don´t know how to put in xx value so i use * please let me know if there is a other way because the value cloud be 40.59 or 81.31 that i need to round in the same function.


    If ($my_number =>*1.25);
    round floor(($my_number)/2.5)*2.5;
    }
    else if round celi(($my_number =<*1.26)/2.5*2.5;

    }


    My results will be 40.00 to 200.00
    The thing is what ever value I get from my calculation 40.00 , 42.50 , 45.00 , 47.50 or 50.00


    My problem is if I use ceil(intval($my_number)/2.5)*2.5; is that if the result is 40.01 it will still round up. :(


    Please let me know if you have a function, and if you cloud please show the echo round_function($my_number); så I know how to echo it on my page.


    Thanks all.
    Best regards
    wizzuriz
     
    wizzuriz, Aug 23, 2011 IP
  14. kptekade

    kptekade Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #14
    I had similar need, like rounding to nearest half like

    0.0 to 0.24 ------ Should be rounded to nearest half as : 0
    0.25 to 0.74 ------ Should be rounded to nearest half as : 0.5

    0.75 to 1.24 ------ Should be rounded to nearest half as : 1
    1.25 to 1.74 ------ Should be rounded to nearest half as : 1.5

    1.75 to 2.24 ------ Should be rounded to nearest half as : 2
    2.25 to 3.74 ------ Should be rounded to nearest half as : 2.5

    and so on...

    here is what I end up implementing...

    $possibleNumber = 1.67;
    echo ' Rounded to nearest half: '. intval(($possibleNumber*2)+0.5)/2;
    Code (markup):
    Test script (only for positive numbers)

    $possibleNumbers = range(0,5,0.01);
    foreach ($possibleNumbers as $possibleNumber) {
       echo '<hr />Possible Number: '. strval($possibleNumber);
       echo ' ------ Rounded to nearest half: '. intval(($possibleNumber*2)+0.5)/2;
    }
    exit;
    PHP:
     
    kptekade, Jan 20, 2014 IP