最近在QT中用到了很多数据格式转换的命令,稍作整理。
//int转double
int a = 1234;
double b;
b = a;//直接赋值就可以
//double转int
double c = 123.456;
int d;
d = c;//d的结果为123,即只取整数部份
d = c*1000;//乘1000将小数消掉即可,注意int位数要求,避免溢出
//int转string
int a = 123456;
string b;
b = std::tostring(a);
//string转int
string c = "123456";
int d;
d = atoi(c.c_str());//string转float 用 atof()
//int转QString
int a = 123456;
QString b;
b = QString::number(a,10,5);//QString::number(a,基底,精度)
//方法2,利用arg()
int a = 123456;
QString b = QString("%1").arg(a);
//QString转int
QString c = "123456";
int d;
d = c.toInt();
//double转QString
double a = 123.456;
QString b;
b = QString::number(a,10,5);//同int
//QString转double
QString c = "123.456";
double d;
d = c.toDouble();//类似int
//string转QString
string a = "123.456";
QString b;
b = QString::fromStdString(a);
//QString转string
QString c = "123,456";
string d;
d = c.toStdString();