문제

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() 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.

다른 팁

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));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top