Trying to add +20% to an amount but...

Discussion in 'PHP' started by x0x, Oct 7, 2009.

  1. #1
    $worth = 400000000*1.2;

    It's giving me 4.8E+8
    Why doesn't it give me a proper number?:confused:
     
    x0x, Oct 7, 2009 IP
  2. jnelson563

    jnelson563 Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Don't know why...

    
    <?php 
    
    $worth = 400000000*1.2;
    
    echo $worth;
    
    ?>
    
    
    PHP:
    Seems to work for me.
     
    jnelson563, Oct 7, 2009 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Must be some server side option then, I'm testing this on my local Mamp server. Anyone knows what setting this could be?
     
    x0x, Oct 7, 2009 IP
  4. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Really weird. It's working with 400000001, but not with 400000000.
     
    x0x, Oct 7, 2009 IP
  5. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    echo intval($worth);
     
    premiumscripts, Oct 7, 2009 IP
  6. Selkirk

    Selkirk Peon

    Messages:
    93
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try this on the Cli
    
    php -r "echo ini_get('precision');"
    
    Code (markup):
    Mine is set at 12 and I get the result of 480000000 for 400000000*1.2.

    If I reduce the precision to fewer digits
    
    ini_set('precision', 2);
    echo 400000000*1.2;
    
    PHP:
    PHP switches to scientific notation, replicating your issue.
     
    Selkirk, Oct 7, 2009 IP
    bizhobby likes this.
  7. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    that's the one - 'E' in this case just means 4.8 * 10 to the power of 8.
     
    szalinski, Oct 11, 2009 IP