How can I use stringstream to print the max amount of decimal places after the dot of a double number (no trailing zeros and no rounding)? For example, if I only want to print up to 5 decimal places:
1 -> 1
1.23 -> 1.23
1.234 -> 1.234
1.2345 -> 1.2345
1.23456 -> 1.23456
1.234567 -> 1.23456
1.2345678 -> 1.23456
1230.2345678 -> 1230.23456 <- Demonstrating that I am not talking about significant digits of the whole number either
etc.
Of all of the tools I see (setw, setprecision, fixed, etc.), I can’t seem to figure this one out. Thanks!
Do you absolutely want to do this with
stringstreamoptions?You could code a
roundfunction like this:and then just print
round(1.2345678, 5).