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
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
How about setprecision()
Code:#include <iomanip> cout << fixed << setprecision(2) << value << endl;
Mr. C: Author and Instructor
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?
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
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
Ahhh cool, thanks.
cout<<setiosflags(ios::fixed | ios::showpoint | ios::right)<<setprecision(2);