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

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

问题描述

我在一个以指数形式表示的文本文件中有一些十进制数字,例如:144.2e-3.我想将值存储在浮点数中.在 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.

推荐答案

toFloat() 应该可以工作.检查您的字符串是否仅包含数字.如果字符串还包含其他内容,例如 "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天全站免登陆