Quote Originally Posted by estanisgeyer View Post
Hi,

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

Qt Code:
  1. QLocale::setDefault(QLocale(QLocale::Portuguese, QLocale::Brazil));
  2. ...
  3. double x = 47.785;
  4. QString str = l.toString(x, 'f', 2);
  5. qDebug() << str; // Output 47,78
To copy to clipboard, switch view to plain text mode 

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.