PDA

View Full Version : Conversion from string to double and precision



Klenje
25th March 2010, 21:32
Hi, I need to read a string from an xml file and then convert it to double. My problem is that I can't find a way to specify precision, so I lose some decimals. Example:

QLocale c(QLocale::C);
qDebug() << c.toDouble("44.48666300");
this prints 44.4867. I need to set the locale since the file uses always the dot as a decimal separator.
It seems that I can specify precision only if I convert a double into a string, not the opposite; do you have any advice? I guess I'll have to use directly some C++ standard function
Thanks in advance

ChrisW67
25th March 2010, 22:31
Your qDebug() line is converting a double to a string in a default format (See the double version of QLocale::toString() and the source to the QDebug << operator):


QLocale c(QLocale::C);
double d = c.toDouble("44.48666300");
qDebug() << d;
qDebug() << QString::number(d);
qDebug() << c.toString(d, 'f', 10);

outputs:


44.4867
"44.4867"
"44.4866630000"
All the possible bits of the floating point representation of your original string are in the double. Note that this may still be an approximation due to the nature of floats.

Klenje
25th March 2010, 23:10
Thanks a lot, I should have thought about qDebug automatic conversion :o

fatecasino
28th December 2010, 17:09
i have an issue with the precision of the conversion.


QString str="0.1";
double x = str.toDouble();//i get 0.1000000001 !!
x= 2*x;//i get 0.200000003

is there a way to correct this?!

nroberts
28th December 2010, 17:17
i have an issue with the precision of the conversion.


QString str="0.1";
double x = str.toDouble();//i get 0.1000000001 !!
x= 2*x;//i get 0.200000003

is there a way to correct this?!

No, not with the standard floating point types anyway. This is an architectural problem caused by base2 floating point numbers. There's simply put, no way to fix it. How you respond to it depends on your needs.

fatecasino
28th December 2010, 17:22
x values in my problem will not be much different that 0.1, or 0.01 (it is a STEP value for a plot function).
Is there a special Qt function to chop off the last 5-6 digits?

nroberts
28th December 2010, 17:25
x values in my problem will not be much different that 0.1, or 0.01 (it is a STEP value for a plot function).
Is there a special Qt function to chop off the last 5-6 digits?

Probably, but why tie yourself to one UI? This kind of thing is more a job for something like boost.format or printf (though I'd go with a Qt version rather than printf).

ChrisW67
28th December 2010, 20:27
i have an issue with the precision of the conversion.
There is no way to perfectly represent 0.1 in binary floating point (without infinite bits). This conversion is as accurate as it can be within the size of the floating point number type (double or float).

x values in my problem will not be much different that 0.1, or 0.01 (it is a STEP value for a plot function).
Is there a special Qt function to chop off the last 5-6 digits?
For display purposes you can format the number, rounded at a certain number digits to the right of the of decimal point, using QString::number() using the 'e', 'E', or 'F' formats (or the QLocale equivalent).