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

    Join Date
    Jun 2000
    Posts
    29
    Rep Power
    0
    Not sure if this is a PHP post or HTML...

    I want to display currency values to 2 decimal places (145.00) on a page generated by php. If the value is returned directly from a double database vale it displays ok, however when I perform any arithmetic on a number it will only display 145.

    Is the result being treated as a string?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Seaside, CA
    Posts
    84
    Rep Power
    15
    $fmt_price = sprintf("%0.2f",$price);
    echo "<P>The price is $$fmt_price.</p>";

    ------------------
    From the day we're born, we're running out of time.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    15
    Try

    printf("%0.2f", $val);
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,643
    Rep Power
    4492
    you can also use number_format().

    string number_format (float number, int decimals, string dec_point, string thousands_sep)

    number_format() returns a formatted version of number. This function accepts either one, two or four parameters (not three):

    If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands.

    If two parameters are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every
    group of thousands.

    If all four parameters are given, number will be formatted with decimals decimals, dec_point instead of a dot (".") before the decimals and
    thousands_sep instead of a comma (",") between every group of thousands.

    ---John Holmes

Similar Threads

  1. Specifying number of decimal places for output
    By CodE-E in forum PHP Development
    Replies: 6
    Last Post: March 4th, 2004, 11:28 AM
  2. Replies: 0
    Last Post: November 12th, 2003, 09:20 AM
  3. Web form field decimal places
    By jwpearson in forum HTML Programming
    Replies: 3
    Last Post: November 11th, 2003, 04:06 PM
  4. Variable number of decimal places
    By pozter in forum PHP Development
    Replies: 14
    Last Post: May 2nd, 2003, 11:26 PM
  5. No. of decimal places for float type
    By OB_redemption in forum Perl Programming
    Replies: 3
    Last Post: October 5th, 2002, 12:55 AM

IMN logo majestic logo threadwatch logo seochat tools logo