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

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

+3
source share
3 answers

toFloat () should work. Make sure your string contains only the number. If the string contains something else, for example, "144.2e-3 a"then toFloat () returns 0. Note that other numbers in the string will result in conversion failure, for example, it QString("144.2e-3 100").toFloat()will return 0.

Additional spaces in the numeric string do not matter, but other characters.

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

Using a string stream, you can convert an exponential number to the required data type.

+1
source

Use QString::toDouble.

Example:

bool ok;
float f = static_cast< float>( QString( "1234.56e-02" ).toDouble( &ok));
0
source

Source: https://habr.com/ru/post/1765190/


All Articles