number of decimal places

S

shez

Hi,

Is there any way to output a double value with different number of
decimal places (apart from using 'sprintf'). I looked up the reference
for C++ streams and found the 'setprecision' manipulator, but this sets
the number of digits before and after the decimal point. I can't seem
to find any manipulator for setting the number of digits after the
decimal point.

Thanks,
-shez-
 
U

Ulrich Achleitner

Sorry guys, i've found the answer. Pls ignore.

you would not need to be sorry, if you were so kind to post the answer you
have found - perhaps others are interested, too.

ps: ignoring, as you suggest, is not possible, because to do so, anybody
must read your original posting, plus your answer to it...
 
S

shez

you would not need to be sorry, if you were so kind to post the
answer you
have found - perhaps others are interested, too.

You can use the 'std::fixed' manipulator before setprecision, like
this:

std::cout << std::fixed << std::setprecision(2) << 1.2345678 <<
std::endl;

This should print to two decimal places. Thanks, -shez-
 
Joined
Feb 19, 2011
Messages
1
Reaction score
0
the above codes work for the newr compilers...if any of you are luking fr things that work with <xyz.h> files only...use the setf as follows


cout.setf(ios::fixed);
cout<<setprecision(4)<<number;


..//this prints exactly 4 digits after a decimal point :D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top