Is there an _easy_ way (i.e. a C++ function or operator) to set the precision to the right of the decimal in a floating point number? i.e. precision of 2 would display 23.5124 as 23.51

Or would I need to write this up myself?

Thanks

Printable View

- 10-14-2002Captain PenguinHow to set decimal precision...
Is there an _easy_ way (i.e. a C++ function or operator) to set the precision to the right of the decimal in a floating point number? i.e. precision of 2 would display 23.5124 as 23.51

Or would I need to write this up myself?

Thanks - 10-14-2002Mister C
How about setprecision()

Code:`#include <iomanip>`

cout << fixed << setprecision(2) << value << endl;

- 10-14-2002Captain Penguin
setprecision sets the total number of digits to be displayed, not the number of digits after the decimal. WIth older compilers it used to work like that, but no more. Stupid, eh?

- 10-14-2002skipper
Quick follow-up to Mr. C's post, make certain to include the 'fixed' manipulator as he has. The standard display precision is six places - as I recall - but includes the value's mantissa in the count. That is, 123.45678 displays as 123.457 (note the rounding of the value).

By using 'fixed', the argument supplied with setprecison() determines the number of places following the decimal.

-Skipper - 10-14-2002Captain Penguin
Ahhh cool, thanks.

- 10-14-2002RoD
cout<<setiosflags(ios::fixed | ios::showpoint | ios::right)<<setprecision(2);