SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Price column - 2 decimals???

    Hi,
    i have a field in the db that is set to 'float' which holds a price, but when you enter say 444.50.... it saves it as 444.5 and therefore displays on the page as 444.5.

    Do i need to change the field type to something else? or can i just rewrite this below so it shows 2 decimals, if so how?

    <?php echo $row_jobs['price']; ?>

    thanks.

  2. #2
    SitePoint Addict Latox's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php echo number_format($row_jobs['price'],2); ?>
    http://php.net/manual/en/function.number-format.php
    :-)

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's all it was, i feel so bad now
    thanks Kyle...

  4. #4
    SitePoint Addict Latox's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem, number_format is also a great tool to format numbers which are higher than 999.. for example:

    PHP Code:
    $price 5000;
    echo 
    $price// this shows 5000
    echo number_format($price); // this shows 5,000 with the comma 
    :-)

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    just a word of caution, FLOAT is an approximate datatype

    if your prices always need to be accurate to the penny, be aware that with FLOAT you run the risk of inaccuracy in calculations

    use DECIMAL instead
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •