CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2006
    Posts
    162

    Output formatting

    Beginner question, how would I format output's to limit decimal places to 2 (0.00), and add 0's when neccisary (1 to 1.00, 1.2 to 1.20) ?

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Output formatting

    Like this?
    Code:
    float f = 100.3f;
    printf("%.2f", f);
    - petter

  3. #3
    Join Date
    Feb 2006
    Posts
    162

    Re: Output formatting

    no i don't think so... the variables i want formatted aren't predefined, they are calculated by the application.

  4. #4
    Join Date
    Aug 2005
    Posts
    132

    Re: Output formatting

    The answer is still valid,

    Code:
    printf("%.2f", f);
    Puts your floating point number into the output buffer as a string of the number to two decimal places.

  5. #5
    Join Date
    Feb 2006
    Posts
    162

    Re: Output formatting

    Hm, thanks for the help, but couldn't I just use the setprecision manipulator?
    like for dollar ammounts i'd have to used fixed along with setprecision in such a manner

    Code:
    double dollar = 342.3287
    cout << setprecision(2) << fixed << dollar << endl;
    resulting in 342.32 being displayed

    but thanks, both methods work great!
    Last edited by 80Degrees; February 18th, 2006 at 10:11 PM.

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

Featured