double转int精度会丢失:
double a = 123.45;
int b = a * 1000;
cout << "1.a=" << a << ",b=" << b << endl;
//结果为:a=123.45,b=123450
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main()
{
double a = 123.45;
int b = a * 1000;
cout << "1.a=" << a << ",b=" << b << endl; //1.a=123.45,b=123450
// 方法1:round()
int c = round(a * 100); // 方法1:round()函数 a=123.45,c=12345
cout << "方法1:round()函数 a=" << a << ",c=" << c << endl;
// 方法2:to_string()
string str = std::to_string(a * 100);
cout << "转换字符str=" << str << endl; //转换字符str=12345.000000
int d = atoi(str.c_str()); // 字符串转整数
// 方法2:to_string() a = 123.45,d = 12345
cout << "方法2:to_string() a=" << a << ",d=" << d << endl;
system("pause");
return 0;
}