This is what std::round()
and std::trunc()
are for. std::trunc()
finds the nearest integer that is not greater than the input. Since you want just 1 decimal place, multiply by 10 (just like @Retired Ninja wrote) before passing it to the function:
float n = 0.948245;
float result = std::trunc(n*10) / 10.0f;
That being said, I'd caution you that float
is not very precise.
See the following example in wandbox:
float n = 100000.948245;
std::cout << n << "\n";
The output is:
100001
The problem is that the number assigned here to n
is both large and also has a lot of digits after the decimal place. (BTW, "large" for float
means anything far from the 0 to 1 range).
Using double
is a little better, but if you really care about precision even in large numbers, you'd be better off using fix-point math. You really have to take into account the ranges of values you care about.