How to convert a string representing decimal number in exponential form to float in Qt?

丶灬走出姿态 提交于 2019-11-29 15:36:45

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.

Anupama
value = 3.91e+01;
double doubleValue;
stringstream valuestream(value);
valuestream >> doubleValue;

Using a stringstream you can convert exponential number to the datatype we require.

Use QString::toDouble.

Example:

bool ok;
float f = static_cast< float>( QString( "1234.56e-02" ).toDouble( &ok));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!