Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Rounding up 10

  1. #1
    Regular Coder Andy92's Avatar
    Join Date
    Nov 2005
    Location
    Horsham, West Sussex, UK
    Posts
    363
    Thanks
    7
    Thanked 1 Time in 1 Post

    Rounding up 10

    Hi there,

    How can i round a number up to 10? Not the nearest 10. So...

    If i had 3, it would round to 10.

    And if i have 107, it would go to 110.

    And if i had 102 it would go to 110.

    And if i had 100 it would stay at 100.

    And if i had 94 it would go to 100.

    How can i do this?

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    PHP Code:
    $number 3;
    $rounded ceil($number/10)*10;
    //$rounded=10; 
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    PHP Code:
    $var 102;

    echo 
    round($var, -1); 

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 315 Times in 307 Posts
    We have a winner (actual output from the equations in the posts) -
    Code:
    Gjay's -
    3 = 10
    107 = 110
    102 = 110
    100 = 100
    94 = 100
    
    PappaJohn's -
    3 = 0
    107 = 110
    102 = 100
    100 = 100
    94 = 90
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Of course, mine rounds and not rounds up

    What I was thinking of (but didn't type) was:
    PHP Code:
    $var 102;

    echo 
    round($var 5, -1); 

  • #6
    Regular Coder Andy92's Avatar
    Join Date
    Nov 2005
    Location
    Horsham, West Sussex, UK
    Posts
    363
    Thanks
    7
    Thanked 1 Time in 1 Post
    Thanks for all your help. Ive got my script all working now.

    Added to all of your reputation!

  • #7
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Quote Originally Posted by PappaJohn View Post
    Of course, mine rounds and not rounds up

    What I was thinking of (but didn't type) was:
    PHP Code:
    $var 102;

    echo 
    round($var 5, -1); 
    if $var is a multiple of 10, then your code will round it up to the next multiple:
    PHP Code:
    $var 100;
    echo 
    round($var 5,-1);
    // output = 110; 
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #8
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    You're absolutely right.


  •  

    Posting Permissions

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