#include<iostream>
using namespace std;
int myRounding(float x)
{
return int(x + 0.5);
}
int main()
{
float x;
for(x = 0; x < 5; x += 0.3)
{
cout << x << "--->" << myRounding(x) << endl;
}
return 0;
}
结果为:
0--->0
0.3--->0
0.6--->1
0.9--->1
1.2--->1
1.5--->2
1.8--->2
2.1--->2
2.4--->2
2.7--->3
3--->3
3.3--->3
3.6--->4
3.9--->4
4.2--->4
4.5--->5
4.8--->5