Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number with comma in Qt

Tags:

qt

qstring

qt4

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 ?


2 Answers

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
like image 140
Eser Aygün Avatar answered Dec 24 '25 10:12

Eser Aygün


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();
like image 35
alexisdm Avatar answered Dec 24 '25 09:12

alexisdm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!