Need to remove trailing .00 from a price

Discussion in 'PHP' started by qwikad.com, Sep 2, 2014.

  1. #1
    This is what I am using:

    <?php
    $price = $ad['price'];
    echo number_format($price, 2, '.', ',');
    ?>
    Code (markup):
    Prices are displayed like this: 1,500,000.00 or 1,125.00 or 1.00

    I want them to show like this: 1,500,000 or 1,125 or 1

    However I still want to show decimals in prices that are 1.09 or 110.75 or 1,111.99 or whatever.

    I just want to get rid of the decimal zeros.

    Thanks!
     
    Solved! View solution.
    qwikad.com, Sep 2, 2014 IP
  2. #2
    You can use floatval(). However, that will remove all trailing zeros. Another solution would be preg_replace('/\.00/', '', $num).
     
    ThePHPMaster, Sep 2, 2014 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,249
    Likes Received:
    1,690
    Best Answers:
    31
    Trophy Points:
    475
    #3
    Ah, ok. I did this number and it seems to be working:

    <?php
    $price = $ad['price'];
    $price = number_format($price, 2, '.', ',');
    $price = preg_replace('/\.00/', '', $price);
    echo $price;
    ?>
    Code (markup):
    Probably not the best way.

    Thanks for the help.
     
    qwikad.com, Sep 2, 2014 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,249
    Likes Received:
    1,690
    Best Answers:
    31
    Trophy Points:
    475
    #4
    I set your answer as the best answer for pointing me in the right direction. I am sure there are other ways of fixing this issue but as of now I am happy with what I've got. Thanks!
     
    qwikad.com, Sep 3, 2014 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Regular expressions are useful for complicated things, but for this, a simple str_replace() would have sufficed (and would also have been faster).
     
    nico_swd, Sep 3, 2014 IP
    ThePHPMaster and qwikad.com like this.
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    I think that could probably be condensed, to something like this:
    
    <?php
    echo str_replace('.00','',number_format($ad['price'],2,'.',','));
    ?>
    
    PHP:
    Unless you need to use the $price-variable again somewhere else, no need to create another variable to hold an array-value.
     
    Last edited: Sep 3, 2014
    PoPSiCLe, Sep 3, 2014 IP
  7. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #7
    Everyone gotta give their 2 cents.
     
    HuggyStudios, Sep 3, 2014 IP
  8. pmf123

    pmf123 Notable Member

    Messages:
    1,449
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    215
    #8
    pmf123, Sep 15, 2014 IP