I have a quick question regarding precision printing output to cout. I need a way to print a MINIMUM number of decimal place digits to cout and also possibly have a maximum number of decimal places printed. Also, I need a way to "undo" if you will a call to setprecision.

So here is my problem, if a number is an integer, such as 2, I want to be able to print it out as 2.0. But the problem I am having is that if I do:

cout << fixed << setprecision(1) << number;

If I do this, setprecision will be set to 1 for the rest of my program, but I do not want that. So in theory I want to be able to do these operations in a row:

Print 2 as 2.0 (can do with a fixed precision of 1)
Print 3.75 as 3.75 (can change fixed precision to 2 for this but don't want a precision limit)
Print 3.5 as 3.5 (do not want a precision limit here)

So my main problem is, I want to be able to reset a call to setprecision after I use it so that only the precision of the current call is affected. I have tried the following commands to reset the flags:

resetiosflags(ios::fixed); 
	cout.flags(previous_flags);

But these only seem to affect whether the flag is set to fixed or scientific and it does not undo the call to setprecision.

I guess my main question is, is it possible to undo a call to setprecision and reset it to the default value it was at? If so what call would that be?

Or, is it possible do a setprecision call where I state the minimum number of decimal places to print as well as the maximum number of decimal places to print?

Thank you for any help, if it is somewhat confusing just ask and I can clarify some more.

I want to clarify a little bit, mainly what I would like to do is be able to set the maximum and minimum values of fixed setprecision so that at max 2 decimals are printed out and at minimum 1 is. So with the same command, these three numbers will be printed:

2.0 (actual double number is 2)
3.5 (actual double number is 3.5)
3.75 (actual double number is 3.75 or 3.74958686949 etc)

Where the minimum number of decimal places is 1 but if the number only needs 1 decimal place it won't add a zero (and make it 3.50) but if the number is 3 digits it will cut it off at 3.75.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.