I try to fetch a float number from a QString using toFloat() method but i get 0 for every number i fetch. I think number format is wrong and system does not recognize it, how can i set right format for these kind of numbers : 119,68648 ?
Please see http://qt-project.org/doc/qt-4.8/qlocale.html#toDouble
Here is the example code from the documentation:
bool ok;
double d;
QLocale german(QLocale::German);
d = german.toDouble( "1234,56", &ok ); // ok == true, d == 1234.56
d = german.toDouble( "1.234,56", &ok ); // ok == true, d == 1234.56
d = german.toDouble( "1234.56", &ok ); // ok == false
d = german.toDouble( "1.234", &ok ); // ok == true, d == 1234.0
You can set a locale where the decimal separator is a comma for your application.
Or you can replace commas by dots before doing the conversion to be able to accept both number formats.
float value = yourString.replace(",", ".").toFloat();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With