如何将以指数形式表示十进制数的字符串转换为Qt中的浮点数? [英] How to convert a string representing decimal number in exponential form to float in Qt?

查看:531
本文介绍了如何将以指数形式表示十进制数的字符串转换为Qt中的浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个以指数形式表示的文本文件中有一些十进制数,例如:144.2e-3。我想把值存储在float。在qt中,当我直接使用number.toFloat()方法时返回0。请帮助。

I have some decimal numbers in a text file represented in exponential form Eg: 144.2e-3. I want to store the values in float. In qt it returns "0" when i directly use the "number.toFloat()" method. Please help.

推荐答案

到Float()应该工作。检查您的字符串是否只包含数字。如果字符串还包含其他东西,例如144.2e-3 a,那么toFloat()返回0.注意字符串中的其他数字将导致转换失败,例如 QString(144.2e-3 100)。toFloat()将返回0。

toFloat() should work. Check that your string contains only the number. If the string contains something else too, for example "144.2e-3 a", then the toFloat() returns 0. Note that also other numbers in the string will cause the conversion to fail, for example QString("144.2e-3 100").toFloat() will return 0.

数字字符串中的其他空格无关紧要,但其他字符可以。

Additional whitespace in the number string doesn't matter, but other characters do.

这篇关于如何将以指数形式表示十进制数的字符串转换为Qt中的浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆