Qt中QString转int,float

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ACanswer/article/details/47448129

环境:linux下Qt

错误案例:

QString s = "2.3";
int n;
n = s.toInt();
cout << n <<endl;

结果:

0
结论:

在linux Qt下不能将带小数的字符串直接转为int型,只能按相应格式转化。

正确案例:

QString s1 = "5";
int n;
n = s.toInt();
cout << n << endl;

QString s2 = "2.6";
float f;
f = s2.toFloat();
cout << f <<endl;

结果:

5
2.6


猜你喜欢

转载自blog.csdn.net/ACanswer/article/details/47448129
今日推荐