Printing float numbers

This is a discussion on Printing float numbers within the C++ Programming forums, part of the General Programming Boards category; How can I print a float number, using a fixed precision? In C would be: printf("%.20f",number); Thanks in advance!...

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Printing float numbers

    How can I print a float number, using a fixed precision?
    In C would be:
    printf("%.20f",number);
    Thanks in advance!
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    One way is to use a combination of the setiosflags function and the setprecision function, i.e:
    Code:
    float f = 9.87463f;
    cout << f << endl;
    cout << setiosflags(ios::fixed) << setprecision(3) << f << endl;
    Output is:
    9.87463
    9.875
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Thanks for the reply, but I was talking about fill with 0īs the number, for example:
    Code:
    float f=0.6;
    float f2=0.12;
    printf("%.5f\n%.5f");
    Output:
    0.60000
    0.12000
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
       float f = .56;
       cout.setf(ios::showpoint | ios::fixed);
       cout << f << endl;
       return 0;
    }
    something like this?

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the code that hk_mp5kpdw gave you should do what you want it to...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 08:17 AM
  3. Pointer
    By silicon in forum C Programming
    Replies: 2
    Last Post: 03-25-2004, 01:34 PM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 05:16 PM
  5. passing variables into function and printing them
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-30-2003, 10:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21