SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I round up a decimal and turn it to an integer in php. Say for example if i have

    0.4 i want it to go to 1
    4.3 to go to 5
    3.01 to go to 4

    etc.

    I guess it is using (number_format ($variable, ?))

    but i can't seem to work it out and get weird results. Any ideas?

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use ceil()

    Syntax: int ceil (float number)


    more info: www.php.net/ceil
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    perfect! thanks so much

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just for the sake of it...

    A generic (non php specific) algorithms for rounding numbers:

    Code:
    1) ROUND DOWN
    
       $myInt = (int)$myDouble;
    
    2) ROUND OFF
    
       $myInt = (int)($myDouble + 0.5);
    
    3) ROUND UP
    
       if ($myDouble == (int)$myDouble) {
          $myInt = $myDouble;
       } else {
          $myInt = (int)($myDouble + 1);
       }
    Hmm, I must be bored to be posting useless code

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just for the sake of uselessness:

    ROUND UP SHOULD BE

    PHP Code:

       $myInt 
    = ($myDouble == (int)$myDouble) ? $myDouble : (int)($myDouble 1); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2001
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If we are going to be useless I will join in.

    Round up could be done this way:

    $myInt = $myInt + 1;
    $myInt = (int)$myDouble;
    Marty H.

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou Freddie - elegent use of the ternary operator, however the logic is just the same

    However, given this is a generic algorithm - I forgot to cast to int in one spot which would be important if we were using a strongly typed language and not PHP.
    Code:
    $myInt*=*($myDouble*==*(int)$myDouble)*?*(int)$myDouble*:*(int)($myDouble*+*1);
    Mart H - that code doesn't work as intentded.

    Deskcheck:

    $myInt = 0, $myDouble = 10.123
    Expected Result: $myInt = 11

    line 1)
    $myInt = 0 + 1 = 1

    line 2)
    $myInt = (int)10.123 = 10

    Result == Expected ?
    10 != 11
    Deskcheck failed

  8. #8
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh my.. what have i done

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2001
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freakysid,

    I told you that I was joining in just to be useless.

    Actually there was a small typo there.

    $myDouble = $myDouble + 1;
    $myInt = (int)$myDouble;

    No lets do the deskcheck

    $myInt = 0, $myDouble = 10.123
    Expected Result: $myInt = 11

    line 1)
    $myDouble = 10.123+ 1 = 11.123

    line 2)
    $myInt = (int)11.123 = 11
    Result == Expected ?
    11 = 11
    Deskcheck Succeeds.
    Marty H.

  10. #10
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - so what about if $myDouble = 10.00
    Does the algorithm work then?

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2001
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like I said, I am just trying to be useless!! And doing very good at it if I may say so myself. And to think I have a math degree.

    /Marty slinks away with tail between legs
    Marty H.

  12. #12
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


Bookmarks

Posting Permissions

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