#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 Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,070
    Rep Power
    1965
    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. Banned

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,326
    Rep Power
    0
    Or if you have PHP5 try [PHPNET="money_format"]money_format()[/PHPNET]
  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,247
    Rep Power
    1993
    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