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.

Rounding problem

Discussion in 'PHP' started by kingsoflegend, Jun 23, 2011.

  1. #1
    When I do a very simple floor(200*1.14) I get the result 227 instead of 228. How does PHP manage to screw this up and how can I make it not do that?

    I'm using PHP 5.2.13.
     
    kingsoflegend, Jun 23, 2011 IP
  2. AdsMakeSense

    AdsMakeSense Active Member

    Messages:
    389
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Thats due to the fact that you're flooring the value. Which does not round it.
    Use the round function instead:

    round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )
     
    AdsMakeSense, Jun 23, 2011 IP
  3. web.business

    web.business Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    
    <?php
    if(is_int(200*1.14)) /* if the number is integer dont floor it else floor it */
    echo 200*1.14 ;
    else echo  floor(200*1.14);
    
    ?>
    
    Code (markup):
     
    web.business, Jun 23, 2011 IP
  4. cmsexperto

    cmsexperto Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    kindly check at once you are sure that the multiplier is 1.14 or some thing else like less then 1.14
     
    cmsexperto, Jun 24, 2011 IP
  5. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #5
    I don't really get this. 200*1.14 does equal 228 exactly so why should flooring it give 227. I always thought flooring rounded down to the nearest whole integer although I'm doubting that given AdsMakesSense's comments. Does that mean that even if the current value IS an integer, it'll ignore that and take the next lower integer value? Seems like very odd functionality.

    Maybe it's just too early in the morning to be thinking about PHP and I'm missing something! :)
     
    mfscripts, Jun 25, 2011 IP
  6. cmsexperto

    cmsexperto Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The thing to remember here is that the way a float stores a value makes it very easy for these kind of things to happen. When the 1.14 was multiplied by 200, the actual value stored in the float was probably something like 227.9999999999999999999999999999999999, PHP would print out 228 when the value is displayed but floor would therefore round this down to 227.

    The moral of this story - never use float for anything that needs to be accurate! If you're doing prices for products or a shopping cart, then always use an integer and store prices as a number of pence, you'll thank me for this later :)
     
    cmsexperto, Jun 25, 2011 IP
  7. cmsexperto

    cmsexperto Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Also check this

    echo floor(2*1.14*100);
     
    cmsexperto, Jun 25, 2011 IP
  8. cmsexperto

    cmsexperto Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    So the only solution is,

    <?php
    if(is_int(200*1.14)) /* if the number is integer dont floor it else floor it */
    echo 200*1.14 ;
    else echo floor(200*1.14);

    ?>
     
    cmsexperto, Jun 25, 2011 IP