CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Minor question about output formatting flags

    How do I output a number such that it's never output in scientific notation, but trailing zeros aren't displayed either? I tried

    Code:
    cout << fixed << noshowpoint;
    but it still outputs, say, 90 as 90.00000?
    Old Unix programmers never die, they just mv to /dev/null

  2. #2
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    36

    Re: Minor question about output formatting flags

    One way is setprecision, but I don't know the best way:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
     
    int main(int cargs, char **vargs) {
      cout << setprecision(0) << 90.0 << endl; // outputs "90"
      return 0;
    }
    http://www.cppreference.com/io_flags.html#manipulators

  3. #3
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Minor question about output formatting flags

    Hmm... setprecision() will cause all numbers to be displayed at the same precision, whereas I need the numbers to be displayed at their default precision, but without trailing zeros.

    Basically, I want numbers to be displayed the way they're displayed by default, but without getting into scientific notation when the numbers get big.

    Any suggestions?
    Old Unix programmers never die, they just mv to /dev/null

  4. #4
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    36

    Re: Minor question about output formatting flags

    While I'm not sure exactly which settings you want, you can use resetiosflags to turn off anything you don't want:
    Code:
    cout << resetiosflags(ios_base::showpoint | ios_base::scientific | ios_base::fixed) << ...
    I hope this helps.

    - Bryan

  5. #5
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Minor question about output formatting flags

    Hmm... that doesn't really work either. 9000000000000000000.0 is still displayed as 9e018...

    Basically this is what I need...

    90 to be displayed as 90 (no trailing zeros)
    9e18 to be displayed as 9000000000000000000 (not in scientific form)
    9.85545 to be displayed as 9.85545 (no precision loss)

    Is that possible?
    Old Unix programmers never die, they just mv to /dev/null

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