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

前端 未结 3 857
抹茶落季
抹茶落季 2020-12-21 07:24

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

3条回答
  •  [愿得一人]
    2020-12-21 07:48

    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.

提交回复
热议问题