先来看一下精度丢失的现象:
#include <iostream>
#include <cmath>
using namespace std;int main()
{double a = 74.46;int b = a * 100;cout << "a: " << a << " b: " << b <<endl;return 0;
}
结果:
解决:采用四舍五入的方法
#include <iostream>
#include <cmath>
using namespace std;int main()
{double a = 74.46;int b = round(a * 100);cout << "a: " << a << " b: " << b << endl;return 0;
}
结果:
















