Unsolved toDouble() lost the data
-
I get data from database with this
query.value(0).toString();
after that I convert string to double
query.value(0).toString().toDouble();
But I lost my data
Original data is
123.45
but after toDouble() data is123.5
How can I fix this ?
-
Hi,
Are you sure that your string contains 123.45 ?
-
yes I am sure
-
Just tested with:
QString test("123.45"); qDebug() << test.toDouble() << test.toFloat();
And got the right results.
-
yes your code work but my problem still exists
qDebug()<<query.value(0).toString()<<" - "<<query.value(0).toDouble()<<" - "<<query.value(0).toString().toDouble(); "123.45" - 123.5 - 123.5
-
Also I tested this
QString test(query.value(0).toString()); qDebug() << test << test.toDouble() << test.toFloat(); "123.45" 123.5 123.5
-
You did not lose any data. qDebug() simply displays 4 significant figures by default.
Test this:
qDebug() << (test.toDouble() - 123.5) // Should produce -0.05
Also, you can ask it to display more significant figures:
qDebug() << qSetRealNumberPrecision(10) << test << test.toDouble() << test.toFloat();