Page 1 of 1

how to withhold the digit of decimal fraction?

Posted: Wed Feb 18, 2004 10:36 am
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?

Posted: Wed Feb 18, 2004 12:48 pm
by Dominik Michniewski
use ios::precision(int)
or read about streams in help ...

Best regards
DM

Posted: Wed Feb 18, 2004 3:25 pm
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?

Posted: Wed Feb 18, 2004 5:59 pm
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]

Posted: Wed Feb 18, 2004 7:25 pm
by Krzysztof Duleba
[cpp] cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2); [/cpp]

Posted: Thu Feb 19, 2004 6:17 am
by Morning
Thanks so much