Results 1 to 5 of 5

Thread: Rounding

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default Rounding

    Folks,

    When $_SESSION['sessionCounter'] is 9 and $_SESSION['limit'] is 8, I think that the PHP_ROUND_HALF_EVEN in

    PHP Code:
    $percentage = (round(((($_SESSION['sessionCounter'] / $_SESSION['limit']) -1) * 100), 0), PHP_ROUND_HALF_EVEN); 
    should round $percentage to the even percentage, 12 percent. Instead, it is rounding up, to 13.

    Am I missing something?

    A.
    Last edited by marain; 02-11-2021 at 10:38 PM. Reason: Restore "Resolved" tag, previously removed in error.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,350
    Thanks
    99
    Thanked 112 Times in 110 Posts

    Default

    There appears to be a typo in your code. Try this instead:

    Code:
    <?php
    $_SESSION['sessionCounter']=9;
    $_SESSION['limit']=8;
    $percentage = (round(((($_SESSION['sessionCounter'] / $_SESSION['limit']) -1) * 100), 0, PHP_ROUND_HALF_EVEN));
    print $percentage;
    ?>
    Note the difference in the placement of the parentheses.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default

    James,

    First of all, thank you!

    I'll be happy to try it, unfortunately, it may be a few days before I am able to find the time to do so.

    In comparing your statement against mine, the only semantic change I see is your adding a set of outer parentheses. Is there more that I missed? Regardless, I'll be happy to report results.

    Thanks again.

    A.

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,350
    Thanks
    99
    Thanked 112 Times in 110 Posts

    Default

    It is not very obvious, so here is your code and my code side by side. The parentheses highlighted in yellow was moved to the outside as seen in the second example where it is correct.
    Code:
    $percentage = (round(((($_SESSION['sessionCounter'] / $_SESSION['limit']) -1) * 100), 0), PHP_ROUND_HALF_EVEN);
    Code:
    $percentage = (round(((($_SESSION['sessionCounter'] / $_SESSION['limit']) -1) * 100), 0, PHP_ROUND_HALF_EVEN));
    Out of curiosity, what is the code used for?
    To choose the lesser of two evils is still to choose evil. My personal site

  5. The Following User Says Thank You to james438 For This Useful Post:

    marain (02-06-2021)

  6. #5
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default

    James,

    You have shown me the error of my ways. Thank you!

    The code is used for messages to visitors to my "poker" page who are asking for more poker "hands" than their limit. It is a frivolous "hobby" page on (one of ) my professional web sites.

Similar Threads

  1. JAVA/PHP Rounding Numbers
    By Gulfbreeze in forum Looking for such a script or service
    Replies: 11
    Last Post: 08-25-2010, 07:36 AM
  2. HTML Rounding Numbers
    By Gulfbreeze in forum HTML
    Replies: 3
    Last Post: 08-23-2010, 06:23 PM
  3. Rounding floating results
    By Piotto in forum JavaScript
    Replies: 4
    Last Post: 11-09-2009, 05:41 PM
  4. Resolved Rounding Up?
    By Rocky! in forum JavaScript
    Replies: 2
    Last Post: 01-03-2009, 05:26 AM
  5. Need Rounding Help in javascript pleaseee
    By tarun in forum JavaScript
    Replies: 0
    Last Post: 03-16-2007, 02:13 PM

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
  •