Results 1 to 2 of 2

Thread: Rounding to Halves?

  1. #1

    Thread Starter
    Fanatic Member stickman373's Avatar
    Join Date
    Mar 2001
    Location
    MA
    Posts
    909

    Rounding to Halves?

    Can anyone think of a good way to do this? I'm struggling.

    I'm trying to write a function that will round a number to an integer or .5 for example:

    3.2342 would round to 3.0

    3.3453 would round to 3.5

    3.6345 would round to 3.5

    3.7666 would round to 4.0

    you know what I mean?

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Rounding to Halves?

    This is the best way I can think of doing it.
    PHP Code:
    <?php
        
    function round_half($num)
        {
            
    $frac $num - ( (int) $num);

            if (
    $frac >= 0.75) {
                return ((int)
    $num) + 1;
            } else if (
    $frac >= 0.25) {
                return ((int)
    $num) + 0.5;
            } else {
                return (int) 
    $num;
            }
        }
    ?>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width