I want to trail the output of a double to 2 decimals or less. Consider following example:
#include <stdio.h>
int main() {
printf( "%.2f\n", 2. );
printf( "%.2f\n", 2.5 );
printf( "%.2f\n", 2.25 );
printf( "%.2f\n", 2.125 );
}
The output is
2.00
2.50
2.25
2.12
Is there any way to print the values with less than 2 decimals, in case less are needed? I mean the desired output is
2
2.5
2.25
2.12
Answers with iomanip are allowed. I'm focusing on C++, not C but the example is c-only.
I know, that it is often not possible because double values are not precise, but in my examples they are because all fractional parts are powers of 2