PDA

View Full Version : QString.toFloat() precision.



hickscorp
7th December 2007, 01:49
Hello people,

Let's say i have a QString named szNum containing "4532.361670".
i want to put it in a float, so i'm doing

float fNum = szNum.toFloat();
Then when i do a
qDebug () << fNum the output is "4532.36".
So, from here i have two guesses:
- My number is fine, but qDebug doesn't display it with the precision after 2.
- My number didnt get converted from QString to float with a good precision.

So the question is: is there a QT function to convert such a string to a float with a better precision? Or do i have to split my string on the dot ".", then store the int part in my float, then add to my float the decimal part divided by 1^(Numbers_Of_Digits+1)?

Thanks ^^
Pierre.

magland
7th December 2007, 03:44
The float data type usually consists of 8 bytes of data, and can represent 6 significant digits. If you do the same experiment with, say, 0.4532361670, then I believe the output would be 0.453236. So, don't worry about the number of digits after the decimal point. It's a floating point number, so the decimal point can float. If you need more digits, use double.

jpn
7th December 2007, 08:12
QString str("4532.361670");
qDebug() << qSetRealNumberPrecision(10) << str.toDouble();