Lining up the Decimal Points
-
I would like to output three columns of numbers with the decimal points lined up. I wrote the following piece of code which I thought would do it.
for( int i = 0; i<100; i++ ) { double f1 = (int)(i/20); double f2 = 3.1415\*i; double f3 = 3.1415\*(f2+3); cout.width( 10 ); cout.precision( 4 ); cout << std::left << f1 << " "; cout.width( 10 ); cout << std::left << f2 << " "; cout.width( 10 ); cout << std::left << f3 << endl; }
However, the decimal points are not lined up. Also, I want to do this in C++ not C. That is, I do not want to use, printf, fprintf or sprintf. How do I get the decimal points to line up vertical? Thanks Bob
-
I would like to output three columns of numbers with the decimal points lined up. I wrote the following piece of code which I thought would do it.
for( int i = 0; i<100; i++ ) { double f1 = (int)(i/20); double f2 = 3.1415\*i; double f3 = 3.1415\*(f2+3); cout.width( 10 ); cout.precision( 4 ); cout << std::left << f1 << " "; cout.width( 10 ); cout << std::left << f2 << " "; cout.width( 10 ); cout << std::left << f3 << endl; }
However, the decimal points are not lined up. Also, I want to do this in C++ not C. That is, I do not want to use, printf, fprintf or sprintf. How do I get the decimal points to line up vertical? Thanks Bob
Check if this is helpful.
{
cout << fixed;
for( int i = 0; i<10; i++ ) {
double f1 = (int)(i/20);
double f2 = 3.1415*i;
double f3 = 3.1415*(f2+3);
std::cout << std::setfill (' ') << std::setw (10);
cout.width( 10 );
cout.precision( 4 );
cout << f1 << " ";
cout.width( 10 );
cout << f2 << " ";
cout.width( 10 );
cout << f3 << endl;
}getch(); }
[Delegates] [Virtual Desktop] [Tray Me !]
-Malli...! :rose:**** -
Check if this is helpful.
{
cout << fixed;
for( int i = 0; i<10; i++ ) {
double f1 = (int)(i/20);
double f2 = 3.1415*i;
double f3 = 3.1415*(f2+3);
std::cout << std::setfill (' ') << std::setw (10);
cout.width( 10 );
cout.precision( 4 );
cout << f1 << " ";
cout.width( 10 );
cout << f2 << " ";
cout.width( 10 );
cout << f3 << endl;
}getch(); }
[Delegates] [Virtual Desktop] [Tray Me !]
-Malli...! :rose:****Thanks for the response. However, it failed to produce the results I wanted. Here is part of the output that I got:
0 0 9.425 0 3.142 19.29 0 6.283 29.16 0 9.425 39.03 0 12.57 48.9 0 15.71 58.77
The decimal points are not lined up. What I want is:
0 0.0 9.425 0 3.142 19.29 0 6.283 29.16 0 9.425 39.03 0 12.57 48.9 0 15.71 58.77
In what I want the decimal points are all in the same column.
-
I would like to output three columns of numbers with the decimal points lined up. I wrote the following piece of code which I thought would do it.
for( int i = 0; i<100; i++ ) { double f1 = (int)(i/20); double f2 = 3.1415\*i; double f3 = 3.1415\*(f2+3); cout.width( 10 ); cout.precision( 4 ); cout << std::left << f1 << " "; cout.width( 10 ); cout << std::left << f2 << " "; cout.width( 10 ); cout << std::left << f3 << endl; }
However, the decimal points are not lined up. Also, I want to do this in C++ not C. That is, I do not want to use, printf, fprintf or sprintf. How do I get the decimal points to line up vertical? Thanks Bob
Have you tried removing
std::left
?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Have you tried removing
std::left
?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
I tried it with std::left, without std::left and with std::right. In all three cases it did not work. What I believe the problem is that there is no way to specified a fixed number of digits along with a fixed number of digits to the right of the decimal point. I think I am going to go use sprintf and then send the output the string sprintf produces. It is not the "C++ way" of doing things but it works. I want to thank the group for their responses. Bob
-
I tried it with std::left, without std::left and with std::right. In all three cases it did not work. What I believe the problem is that there is no way to specified a fixed number of digits along with a fixed number of digits to the right of the decimal point. I think I am going to go use sprintf and then send the output the string sprintf produces. It is not the "C++ way" of doing things but it works. I want to thank the group for their responses. Bob
Try adding
cout << setiosflags(ios::fixed)
right before thefor()
loop."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Thanks for the response. However, it failed to produce the results I wanted. Here is part of the output that I got:
0 0 9.425 0 3.142 19.29 0 6.283 29.16 0 9.425 39.03 0 12.57 48.9 0 15.71 58.77
The decimal points are not lined up. What I want is:
0 0.0 9.425 0 3.142 19.29 0 6.283 29.16 0 9.425 39.03 0 12.57 48.9 0 15.71 58.77
In what I want the decimal points are all in the same column.
-
Try adding
cout << setiosflags(ios::fixed)
right before thefor()
loop."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles