Say I have a method returning a double, but I want to determine the precision after the dot of the value to be returned. I don’t know the value of the double varaible.
Example:
double i = 3.365737;
return i;
I want the return value to be with precision of 3 number after the dot
Meaning: the return value is 3.365.
Another example:
double i = 4644.322345;
return i;
I want the return value to be: 4644.322
What you want is truncation of decimal digits after a certain digit. You can easily do that with the
floorfunction from<math.h>(orstd::floorfrom<cmath>if you’re using C++):Still, I think that in some cases you may get some strange results (the last digit being one over/off) due to how floating point internally works.
On the other hand, most of time you just pass around the
doubleas is and truncate it only when outputting it on a stream, which is done automatically with the right stream flags.