Limiting decimal places

Discussion in 'PHP' started by Ntech, Aug 22, 2009.

  1. #1
    Hello,

    The code below returns the value 29.543441772461.

    How do I get it to only show 29.54

    $size = $row['fileSize'];
    $size = $size/1048576;
    Code (markup):
     
    Ntech, Aug 22, 2009 IP
  2. Ntech

    Ntech Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Never mind,

    The first time I tried sprintf() it didn't work. I tried it again and now it works.:eek:
     
    Ntech, Aug 22, 2009 IP
  3. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can also use round($num,$places)
     
    Kyosys, Aug 23, 2009 IP
  4. Ntech

    Ntech Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Is there a reason not to use sprintf?

    Speed is my concern. Is round any faster than sprintf?
     
    Ntech, Aug 23, 2009 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    The best options would be round or number_format.
     
    jestep, Aug 23, 2009 IP