How to convert a string representing decimal number in exponential form to float in Qt?(如何将表示以指数形式表示的十进制数的字符串转换为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中的浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将表示以指数形式表示的十进制数的字符串转换为Qt中的浮点数?
基础教程推荐
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- c++ STL设置差异 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
