#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Floatval - How to display 2 digits after decimal point


    Hi all,
    I'm not too expert in PHP. I've got a code in which it uses floatval. However with floatvalue it displayes only one digit after decimal if there is only one nonzero digit after it.
    E.g.

    0.50 is displayed as 0.5 (incorrect 0 is missing)
    0.55 is displayed as 0.55 (correct if vboth digits are non zero after decimal)

    Can anyone let me how to fix it quickly without going too deep into PHP programming?

    Tx in advance.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,706
    Rep Power
    1959
    take a look on [phpnet=number-format]number-format[/phpnet]

    Comments on this post

    • ManiacDan agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0
    Tx buddy it worked
  6. #4
  7. Prisoner of the Sun

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,309
    Rep Power
    2351
    Or if you have PHP5 try [PHPNET="money_format"]money_format()[/PHPNET]
    .
    :: My blip.fm tunes :: Web Design Feeds :: Web Dev Feeds :: CheatSheets :: PHP :: MySQL :: 13 Moon FB App.

    "All matter is merely energy condensed to a slow vibration. We are all one consciousness experiencing itself - subjectively. There is no such thing as death, life is only a dream. We are the imaginations of ourselves."
    - Bill Hicks


    "Truth is hidden in the subtle nature of the heart of everything, although it is invisible. One cannot see it from inside and neither from the surface. One can only live and experience it."
    - Heart Sutra
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0
    number_format() is great for formatting, but could screw things up if your output isn't expecting a comma (like in an XML-driven chart.

    Another alternative is to use:
    PHP Code:
    $result=round(100*$number)/100 
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0
    number_format() is great for formatting, but could screw things up if your output isn't expecting a comma (like in an XML-driven chart.

    Another alternative is to use:
    PHP Code:
    $result=round(100*$number)/100 
  12. #7
  13. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,128
    Rep Power
    1990
    Originally Posted by BikeMaster
    number_format() is great for formatting, but could screw things up if your output isn't expecting a comma (like in an XML-driven chart.
    That's why there's the optional parameters on number_format. To make a purely numerical value you'd use:

    Code:
    $val = number_format ($val, 2, ".", "");
    That adds in the decimal place, but removes the other separators for thousands, etc.

IMN logo majestic logo threadwatch logo seochat tools logo