the output will be 9.12. What if I wanted to save that as another separate float with displaying it on screen? How would I do this? Any help qould be appreciated. Thank you!
Your question is a little bit unclear but I believe you are asking how to save it the variable with it's precision truncated? That is, save 9.12345 as 9.12 (which is 9.12000)?
You need to use doubles do this.
This is how I do it:
1 2 3 4 5 6 7 8 9 10
inlinevoid tools::SetDoublePrecision(double& value, int precision, double units)
{
double fac = (value < 0.0) ? -1. : 1.;
value = fabs(value);
uint64 tmp = static_cast<uint64>((value/units) * pow(10.,precision));
value = (static_cast<double>(tmp) * pow(10.,-precision))*units;
value *= fac;
}
You can leave out the "units" variable if you want or set it as a default value of 1.0 in the function prototype. I wrote it this way because certain times I want to truncate depending on units (e.g. if the variable represents 9.12345 meters and stored in the program as 9123.45 millimeters and I want the variable in two decimal places with respect to centimeters)
This doesn't work with floats though because float doesn't retain precision in the detail double does. Making var1 and var_truncated floats instead of double results in this: