Thread: Printing Double?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    14

    Printing Double?

    I would like to know how to print double variable if it is assigned a number with alot of digits.

    For example, if i were to cout<<(double)100000000<<endl i would get printed "1e+008" printed out. However, I would like to get "100000000" How would I accomplish this?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Try
    std::cout << std::fixed << 100000000.0 << std::endl;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Thanks. is there a way to limit the number of zeros to the needed length? like...

    double a = 5;
    cout<<fixed<<a<<endl;

    would print out something like...
    5.0000000000

    when simple 5 or 5.0 would be sufficient.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Try std::setprecision.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    cout.precision(1);
    cout << fixed << a << endl;

    e: I'm not quite fast enough

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    C++ unfortunately makes formatting a clumsy affair. In C++ a programmer uses tiny objects called manipulators to change the way the stream behaves. You can find the manipulators that C++ has standard in any reference page: C++ I/O Flags [C++ Reference]. It is important that you become familiar with these.

    You can also make your own manipulators.

    It is also nice to remove the format after you are done:

    cout.resetiosflags( ios_base::showpoint );

    Forgetting to do that can make output look strange sometimes when you don't expect it.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is also Boost's Format library which tries to combine the best of both worlds: The Boost Format library
    Though I suck at using it (mainly because the documentation is so poor). I only use it for basic formatting.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    11
    Nvm...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from file and printing double data
    By hammari in forum C Programming
    Replies: 4
    Last Post: 07-14-2009, 07:02 AM
  2. Structures within structures for a database
    By Holtzy in forum C Programming
    Replies: 2
    Last Post: 04-30-2008, 07:06 AM
  3. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 09:27 PM