Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    displaying dollars and cents

    Is there a PHP function to display a variable to two decimal places?
    EG if $price="3.5" I still want it to display as "3.50".
    Last edited by aslum; 09-10-2009 at 08:57 PM. Reason: Resolved

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    yes,

    PHP Code:
    number_format(3.5,2); 

  • Users who have thanked angst for this post:

    aslum (09-10-2009)

  • #3
    New Coder
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks! I knew there had to be a function to do it.
    You can also do:
    number_format($price,"2",".",",")
    if you want commas at the thousands/millions/etc.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    your welcome!
    it will automatically add in commas where needed without the extra arguments.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    money_format()
    Uses more memory and time, but will adhere to the setlocal value if you want to change the currency.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    money_format() only works on linux servers. You're most likely on one, but keep that in mind if you use it.

  • #7
    Regular Coder
    Join Date
    May 2009
    Posts
    143
    Thanks
    14
    Thanked 1 Time in 1 Post
    php can do anything ;D


  •  

    Posting Permissions

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