Results 1 to 5 of 5

Thread: Need to remove trailing .00 from a price

  1. #1
    Join Date
    Oct 2012
    Posts
    180
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Need to remove trailing .00 from a price

    This is what I am using:

    Code:
    <?php
    $price = $ad['price'];
    echo number_format($price, 2, '.', ',');
    ?>
    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!

  2. #2
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    +0 to the value
    PHP Code:
    <?php
    $price 
    $ad['price'] + 0;
    echo 
    number_format($price2'.'',');
    ?>
    If that doesn't work use

    PHP Code:
    <?php
    $price 
    = (float)$ad['price'];
    echo 
    number_format($price2'.'',');
    ?>
    to cast it from a string to a float
    Don't mind me
    number_format forces the decimals on.
    Last edited by keyboard; 09-04-2014 at 12:33 AM.

  3. #3
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Or maybe str_replace();
    PHP Code:
    Code:
    <?php
    $price 
    $ad['price'];
    echo 
    str_replace('.00'''number_format($price2'.'','));
    ?>
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  4. #4
    Join Date
    Oct 2012
    Posts
    180
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default

    I tried what you guys suggested but it didn't do the trick. I ended up going the long route:

    Code:
    <?php
    $price = $ad['price'];
    $price = number_format($price, 2, '.', ',');
    $price = preg_replace('/\.00/', '', $price);
    echo $price;
    ?>
    I probably should change that preg_replace to str_replace eventually. Does the code above look silly to you?

  5. #5
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Looks fine - very readable.

    Yes, changing it to str_replace() will be slightly faster for a simple replacement - preg_replace() is more powerful and better suited to more complex replacements. A bit of speed test info is here: http://benchmarks.ro/2011/02/str_rep...-preg_replace/
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

Similar Threads

  1. trailing mouse under images
    By toplessjan in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 01-25-2013, 04:49 PM
  2. Replies: 1
    Last Post: 02-08-2012, 08:26 PM
  3. Get price from DB echo different price
    By PatrikIden in forum PHP
    Replies: 18
    Last Post: 11-25-2011, 09:32 PM
  4. Apache - No Trailing Slash
    By X96 Web Design in forum Computer hardware and software
    Replies: 6
    Last Post: 12-31-2009, 01:05 AM
  5. Mouse cursor and trailing
    By gjoaquin in forum JavaScript
    Replies: 3
    Last Post: 07-28-2005, 09:38 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •