Another Question

Discussion in 'PHP' started by kc3, May 10, 2005.

  1. #1
    I have another stupid question, lol, if you have a number that's one decimal place how do you add a 0 after it. For example how do you determine if variable $number (say $number = 19.5 ) has one decimal place and make it add a 0 after it to make $number = 19.50. (lol, there is an actual reason for this) Though I also have the same question for the reverse, say $number = 19.539 how would I be able to make $number = 19.54?
     
    kc3, May 10, 2005 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,807
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #2
    sarahk, May 11, 2005 IP
  3. kc3

    kc3 Peon

    Messages:
    857
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, I'll check it out.
     
    kc3, May 13, 2005 IP
  4. neterslandreau

    neterslandreau Peon

    Messages:
    279
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    neterslandreau, May 14, 2005 IP
  5. kc3

    kc3 Peon

    Messages:
    857
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    All of those worked great, but for some reason I still have a problem when a price is $10.50 it shows it as $10.5, if it's $7.00 it shows it as $7. I need the extra zeros, how do I do this is there is only one or less digits after the decimal?
     
    kc3, May 26, 2005 IP
  6. draculus

    draculus Peon

    Messages:
    63
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You should use the money_format() function. It's purpose is to do what you want to do. You might also look at number_format().
     
    draculus, May 27, 2005 IP
  7. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Use sprintf:

    <?= sprintf("%.02f", $number) ?>

    J.D.
     
    J.D., May 27, 2005 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,807
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #8
    sarahk, May 27, 2005 IP
  9. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #9

    Number format returns a string.

    Sprintf type casts it as a float that you can still do math with.
     
    noppid, May 27, 2005 IP
  10. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Just as kc3 says, number_format won't format 10.5 as 10.50.

    J.D.
     
    J.D., May 27, 2005 IP
  11. kc3

    kc3 Peon

    Messages:
    857
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thank you for all of your help.
     
    kc3, May 27, 2005 IP