| | |
fixed number of decimals
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 42
Reputation:
Solved Threads: 0
Hi,
If I want to print several numbers, say 12.3, 24.038, 154.9181, 0.4778--anyway, just all kinds of numbers, how can I print all of them with fixed number of decimals, say two decimals, that is, the output will be: 12.30, 24.03, 154.91, 0.47... ? The "setprecision(...)" doesn't work because it specifies the number of "significant" digits, rather than the number of digits after the decimal point.
Thank you in advance!
If I want to print several numbers, say 12.3, 24.038, 154.9181, 0.4778--anyway, just all kinds of numbers, how can I print all of them with fixed number of decimals, say two decimals, that is, the output will be: 12.30, 24.03, 154.91, 0.47... ? The "setprecision(...)" doesn't work because it specifies the number of "significant" digits, rather than the number of digits after the decimal point.
Thank you in advance!
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
cout << fixed << setprecision (2);
try that
(seeing as I can't delete a post, to prevent redundancy, I'll give an alternate solution)
Hi, in the case of setprecision() not working, try the following:
cout.setf(ios::fixed); // prevents e-notation
cout.setf(ios::showpoint); //always shows decimal point
cout.precision(2); // with 2 being the number of decimals
using that, here's what happened in my test program:
4 inputs
1.234
1.23
123.456
1234
and then just used cout to put them on the screen
1.23
1.23
123.46 // rounded
1234.00
See if that works for you
~J
Hi, in the case of setprecision() not working, try the following:
cout.setf(ios::fixed); // prevents e-notation
cout.setf(ios::showpoint); //always shows decimal point
cout.precision(2); // with 2 being the number of decimals
using that, here's what happened in my test program:
4 inputs
1.234
1.23
123.456
1234
and then just used cout to put them on the screen
1.23
1.23
123.46 // rounded
1234.00
See if that works for you
~J
Last edited by jesseb07; Feb 22nd, 2008 at 11:54 pm.
![]() |
Similar Threads
- Program Seems to be right but still some confusion on output into columns (C++)
- help using decimals in program (C++)
- Student requesting help - Please! (C++)
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





