Results 1 to 4 of 4

Thread: Format Doubles

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Format Doubles

    I want to format a double variable to have two decimal places no matter what. Is there a way to do this without lengthy code?

    Example:

    Code:
    4      -> 4.00
    4.4   -> 4.40
    4.44 -> 4.44
    etc..
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    ostringstream oss;
    oss.precision(2); // play with this :D
    oss << num;
    You can use any of the usual stream manipulators...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    jim mcnamara
    Guest
    in standard C - printf("%8.2f",mydouble);

    the 8 is the number of positions in front of the decimal point, the 2 is the number after it.

  4. #4

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I know this was a few months ago, but thanks for the help.

    I don't remember what I went with, but nowadays I use setprecision(2).

    Man I really asked some noob questions back in the day.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width