php limit float character

Discussion in 'PHP' started by dracula51, Oct 9, 2009.

  1. #1
    look at the codes plz
    $a = '29';
    $b = '7';
    
    $c = $a/$b;
    
    echo $c;
    PHP:
    very simple thing. we divided $a with $b (29 divided by 7)

    it will show the result : 4.1428571428571428571428571428571

    very long...isnt it?

    well i want it to show just 4.14
    that means only two number after the point (.)

    how can i do this...plz help me
     
    dracula51, Oct 9, 2009 IP
  2. niil

    niil Greenhorn

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #2
    try this

    $c = number_format($c, 2, '.', '');
     
    niil, Oct 9, 2009 IP
  3. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #3
    number_format will only give you 4.1428571428571428571428571428571.
    its not going to make it 4.14.

    try number_format and the substr to get what you need.

    Good luck.

    btw

    $a = '29';
    $b = '7';

    $a and $b are strings not integers.

    you cant divide 2 strings.(lol/haha=:p)

    you need

    $a = 29;
    $b = 7;
     
    astrazone, Oct 9, 2009 IP
  4. travelmate99

    travelmate99 Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    number_format should work fine because I am using it.
    If you have problem, you can try to round($c*100)/100
     
    travelmate99, Oct 9, 2009 IP
  5. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    tnx niil...that works fine

    @astrazone: i didnt need to use substr...only number_format works fine. did u try only number_format urself? im confussed if it depends on php version !!
    btw...strings or integers...whatever...both works fine in php 5.2.9
     
    dracula51, Oct 9, 2009 IP
  6. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #6
    astrazone, Oct 9, 2009 IP
  7. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yeah travelmate99 said correctly number_format will do the job
     
    lmao, Oct 9, 2009 IP