Conversion from string to double and precision
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:
Code:
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
Re: Conversion from string to double and precision
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):
Code:
double d = c.toDouble("44.48666300");
qDebug() << d;
qDebug() << c.toString(d, 'f', 10);
outputs:
Code:
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.
Re: Conversion from string to double and precision
Thanks a lot, I should have thought about qDebug automatic conversion :o
Re: Conversion from string to double and precision
i have an issue with the precision of the conversion.
Code:
double x = str.toDouble();//i get 0.1000000001 !!
x= 2*x;//i get 0.200000003
is there a way to correct this?!
Re: Conversion from string to double and precision
Quote:
Originally Posted by
fatecasino
i have an issue with the precision of the conversion.
Code:
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.
Re: Conversion from string to double and precision
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?
Re: Conversion from string to double and precision
Quote:
Originally Posted by
fatecasino
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).
Re: Conversion from string to double and precision
Quote:
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).
Quote:
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).