QT 关于int、double、string、QString格式相互转换的方法
1,int与double
//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位数要求,避免溢出
2,int与string
//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()
3,int与QString
//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();
4,double与QString
//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
5,string与QString
//string转QString
string a = "123.456";
QString b;
b = QString::fromStdString(a);
//QString转string
QString c = "123,456";
string d;
d = c.toStdString();