LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to format a long double number in C++ to display as many digits as possible. (https://www.linuxquestions.org/questions/programming-9/how-to-format-a-long-double-number-in-c-to-display-as-many-digits-as-possible-378891/)

TruongAn 11-01-2005 06:01 AM

How to format a long double number in C++ to display as many digits as possible.
 
Hi guys.
I am studying C++.
And I have wrote a program which calculate with long double number.
My goal is not to dis play the point in the number.
I mean: dislay
Code:

199999
not
Code:

2.00e+3
.
How can I do so with C++.

naf 11-01-2005 09:30 AM

Use a fixed point style and set the precision to 0:

Code:

#include <cstdio>
#include <iostream>

using namespace std;

int main( int argc, char **argv )
{
    double d = 1234.56789012345678901234567890;
    cout << "Double (raw) = " << d << endl;  // Defaults to 6 significant digits.
    cout.setf( ios_base::fixed, ios_base::floatfield );
    cout.precision( 0 );
    cout << "Double (adj) = " << d << endl;

    return 0;
}

Results:
Code:

Double (raw) = 1234.57
Double (adj) = 1235


TruongAn 11-03-2005 07:45 AM

It work, thank you!

TruongAn 11-08-2005 10:19 AM

Please explain the code for me, I am new to C++

naf 11-09-2005 10:11 AM

The code modifies the output stream directly for the subsequent write by changing the values of base_ios. If you want the change to remain persistent, you need to use manipulators. The concept is well documented in many books including Stroustrups. But for one good online resource look at:
http://www.cplusplus.com/ref/iostream/ios_base


All times are GMT -5. The time now is 04:31 AM.