PDA

View Full Version : Problems with rounding



estanisgeyer
14th December 2009, 18:54
Hi,

I'm having a problem rounding. See the following code:



QLocale::setDefault(QLocale(QLocale::Portuguese, QLocale::Brazil));
...
QLocale l;
double x = 47.785;
QString str = l.toString(x, 'f', 2);
qDebug() << str; // Output 47,78


The expected result would be 47.79, is correct in not making the rounding? I noticed that he only does when rounding the third decimal place is greater than 5.

Thanks,

Marcelo E. Geyer

schnitzel
14th December 2009, 22:30
if you try 47.795, you will get 47.80.

if I'm not mistaken, then this is called banker's rounding.

I am not sure if/how that can be changed to mathematical rounding.

bmhautz
14th December 2009, 23:53
Hi,

I'm having a problem rounding. See the following code:



QLocale::setDefault(QLocale(QLocale::Portuguese, QLocale::Brazil));
...
QLocale l;
double x = 47.785;
QString str = l.toString(x, 'f', 2);
qDebug() << str; // Output 47,78


The expected result would be 47.79, is correct in not making the rounding? I noticed that he only does when rounding the third decimal place is greater than 5.

Thanks,

Marcelo E. Geyer

Actually, this is probably not a problem with Qt but with the underlying compiler float operations. 47.785 is probably being represented as 47.78499842... (for example) in the actual register, and so, is being round down to 47.78 since it is not greater than or equal to 47.785.

Here's a Wikipedia entry about floating point numbers that might help you: http://en.wikipedia.org/wiki/Floating_point

Hope that helps.

schnitzel
15th December 2009, 20:20
Actually, this is probably not a problem with Qt but with the underlying compiler float operations. 47.785 is probably being represented as 47.78499842... (for example) in the actual register, and so, is being round down to 47.78 since it is not greater than or equal to 47.785.

Here's a Wikipedia entry about floating point numbers that might help you: http://en.wikipedia.org/wiki/Floating_point

Hope that helps.

good article.
I guess a workaround would be to first use rounding to 3 decimals, then round to 2?

estanisgeyer
16th December 2009, 00:50
I'm seeing the class knumber api KDE _round the method of this class can help with correct rounding. Follow the link:
http://api.kde.org/4.x-api/kdeutils-apidocs/kcalc/html/knumber_8cpp_source.html