I was looking for a method to round float numbers in c++ this morning and I found this answer solve my problem.
However, I notice something unusual to me. When I try to round certain float numbers to two decimal places, it seems like numbers such as 1.075 and 1.895 follow different rounding rules. Specifically, with the following simple code:
#include <iostream>
#include <iomanip>
int main(int argc, char** argv)
{
float testme[] = { 1.07500, 1.89500, 2.70500, 3.47500};
std::cout << std::setprecision(2) << std::fixed;
for(int i = 0; i < 4; ++i)
{
std::cout << testme[i] << std::endl;
}
return 0;
}
The result I have is
1.08
1.89
2.70
3.47
So 1.075 turns to 1.08 while 1.895 becomes 1.89. This really confused me. I would appreciate some explanation. Thanks!