Round a float to a regular grid of predefined points
c++, rounding
Solution
As long as your grid is regular, just find a transformation from integers to this grid. So let's say your grid is
0.2 0.4 0.6 ...
Then you round by
float round(float f)
{
return floor(f * 5 + 0.5) / 5;
// return std::round(f * 5) / 5; // C++11
}
Problem
I want to round a float number to a given precision, for example : ``` 0.051 i want to convert it to 0.1 0.049 i want to convert it to 0.0 0.56 i want to convert it to 0.6 0.54 i want to convert it to 0.5 ``` I cant explain it better, but the reason for this is to translate a point location (like 0.131f, 0.432f) to the location of tile in a grid (like 0.1f, 0.4f).