CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 31
  1. #16
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Double output problem

    According to the documentation, setprecision(2) would arrange that. The rest of the padding you'd use setw() for.

  2. #17
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    Re: Double output problem

    Quote Originally Posted by Lindley
    According to the documentation, setprecision(2) would arrange that. The rest of the padding you'd use setw() for.
    I tried that, no dice. Still shows the E notation. This is really annoying. I guess i just have to go find function to round to significant figures. I could use Ceil or Floor, but I want the decimals. heh.
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

  3. #18
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Double output problem

    There's no way it should be using scientific notation if you put the fixed manipulator in there....?

  4. #19
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    Re: Double output problem

    of course it doesn't with Fixed. However, I want a solution that doesn't add zeros. So i'm at the point i'm hacking together a function for it. Now to figure out why converting it to a string yields xxxx.xx but converting it back to a double via istringstream yields the E notation.
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

  5. #20
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Double output problem

    Am I right in assuming that you don't want trailing zeros?

    123.45
    765.2
    7868

    instead of

    123.45
    765.20
    7868.00
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #21
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Double output problem

    Code:
    cout << "text" << fixed << right << setw(x) << setprecision(0) << value << endl;
    Should specify no trailing zeros. Using setprecision(2) will yield standard money notation. The rest of your formatting should be controlled via setw(); *only* the minimum number of decimal places should be controlled via setprecision().

  7. #22
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    Re: Double output problem

    That's what I thought. However, it doesn't work. It just gets put in scientific notation. Here's a screenshot.

    http://i96.photobucket.com/albums/l190/c89c/Capture.jpg

    It shows E even with 2 setprecision and setw(7).
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

  8. #23
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Double output problem

    I suppose it's possible the ordering of the manipulators matters. Try putting "fixed" both to the right and left of setprecision (alternately, not at the same time).....

  9. #24
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Double output problem

    Don't know what your doing but...
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
    	double d = 1900.0;
    	
    	std::cout << std::fixed << std::setw(8) << std::setprecision(2) << d << std::endl;
    
    	return 0;
    }
    Gives me '1900.00'

    Changing to std::setprecision(0) gives me '1900'

    Can you show the line you are actually using rather than having us guess?
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  10. #25
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    Re: Double output problem

    Code:
    cout << "\t\tMonthly Payment:\t" << setprecision(2) << setw(7) << pmt << endl;
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

  11. #26
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Double output problem

    ....why is std::fixed missing again? You *do* realize that all those extra zeros you were complaining about were the result of having setprecision(>2) before, don't you?

  12. #27
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    Re: Double output problem

    Yes, but this particular line never had setprecision greater than 2 and i've checked and it has exactly two decimal places when it's returned. For some reason, it gets retrned and then output does something weird and puts it in scientific notation.

    For example:

    Before Return: 1875.11
    After Return: 1875.11

    When I just do
    Code:
    cout << pmt << endl;
    I get 1875.11. However, once it reaches the line setprecision and setw it does scientific notation for some reason.

    Edit: I just tried putting fixed in and it fixes the problem. I'm still wondering why setprecision and setw would make such a difference.
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

  13. #28
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Double output problem

    http://www.cplusplus.com/reference/i...precision.html

    If you don't specify fixed-point, then setprecision specifies the maximum number of meaningful digits to display. If the value is 1875.11, and you say setprecision(2), then it's only going to display 1 and 8----and the best way to do that is to write 1.8e3.

    Of you do specify fixed-point, then the meaning of setprecision() changes to be the number of digits after the decimal to display.

  14. #29
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Double output problem

    Code:
    double pmt = 1875.11;
    
    cout << "\t\tMonthly Payment:\t" << setprecision(2) << setw(7) << pmt << endl;
    Gives '1.9e+003'
    Code:
    double pmt = 1875.11;
    
    cout << "\t\tMonthly Payment:\t" << fixed << setprecision(2) << setw(7) << pmt << endl;
    Gives '1875.11'
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  15. #30
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    Re: Double output problem

    Quote Originally Posted by Lindley
    http://www.cplusplus.com/reference/i...precision.html

    If you don't specify fixed-point, then setprecision specifies the maximum number of meaningful digits to display. If the value is 1875.11, and you say setprecision(2), then it's only going to display 1 and 8----and the best way to do that is to write 1.8e3.

    Of you do specify fixed-point, then the meaning of setprecision() changes to be the number of digits after the decimal to display.
    Somehow, that seems like something our professor should have distinguished. He just used it like it determined decimal places and didn't affect the number at all.
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

Page 2 of 3 FirstFirst 123 LastLast

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