how to withhold the digit of decimal fraction?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

how to withhold the digit of decimal fraction?

Post by Morning »

My english is not good,so i don't know how to say,is it use "withhold"?i mean
a=12.33342,b=1.3321
if i want to withhold the 2 two digits of the decimal fraction
i wanna output a=12.33,b=1.33
so is there any function in cout can control it?just like cout.precision(),which can control the decimal fraction's valid digits?
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

use ios::precision(int)
or read about streams in help ...

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

if i use the procision(3) function
a=12.33342,b=1.3321
the output will be
12.3 1.33
but what i need is
12.333 1.332

which function i shall use?
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

In C:
[c]printf ("%.3lf\n", 1.3456789);[/c]
where the .3 signifies the number of digits after the decimal point.

In C++:
[cpp]std::cout << std::fixed << std::setprecision(3) << 1.3456789 << endl;[/cpp]
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

[cpp] cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2); [/cpp]
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

Thanks so much
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Post Reply

Return to “C++”