How to use setprecision in cpp for floating point only
2 answers
you can use std::fixed
std::cout << std::fixed << std::setprecision(4) << 31.14159 << endl ;
and you will get the result as 31.1416
When you add std::fixed
, std::setprecision(4)
will act on the decimal part and you will get the values 4
after the decimal point.
A std::setprecision()
will take effect for the entire number. But when you add std::fixed
along with it, it std::setprecision()
will only use the decimal part of the number.
+1
source to share