Results 1 to 5 of 5

Thread: Problems with rounding

  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Problems with rounding

    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

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems with rounding

    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.
    Last edited by schnitzel; 14th December 2009 at 21:32. Reason: updated contents

  3. #3
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with rounding

    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.

  4. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems with rounding

    Quote Originally Posted by bmhautz View Post
    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?

  5. #5
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: Problems with rounding

    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-...pp_source.html

Similar Threads

  1. problems creating toolbar...(do see the attachment)!
    By sumit in forum Qt Programming
    Replies: 15
    Last Post: 10th September 2008, 11:23
  2. toDouble() and rounding problem
    By sadjoker in forum Newbie
    Replies: 10
    Last Post: 28th August 2008, 12:47
  3. flicker and wierd resize problems ...
    By momesana in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2008, 18:00
  4. Canvas problems
    By Tommytrojan in forum Qt Programming
    Replies: 22
    Last Post: 9th May 2006, 16:46
  5. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.