Results 1 to 3 of 3

Thread: Rounding in QT

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Rounding in QT

    Wellcome all!

    I'm writting the software for invoicing. Essential to this is to use mathematical rounding to two decimal places.

    I've tried to use:
    Qt Code:
    1. QString::number(value,'f',2).toDouble();
    To copy to clipboard, switch view to plain text mode 
    but - it doesne't work!

    for example:
    2582.3250 return me: 2582.32 -it's wrong! It should be 2582.33

    what am I doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Rounding in QT

    You are falsely assuming any floating point number can be represented by the float datatype.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Rounding in QT

    The closest approximation of 2582.3250 as a float is 2582.324951171875. Perhaps it is obvious now why it rounds the way it does.
    Have a play with this here

    You are also assuming there is only one way to round: that is 0.5 rounds up. The IEEE754 default rounding method is round-to-even so 1.125 rounds to 1.12, and 1.375 rounds to 1.38, and that is what Qt does also:
    Qt Code:
    1. qDebug() << QString::number(1.125, 'f', 2); // outputs "1.12"
    2. qDebug() << QString::number(1.375, 'f', 2); // outputs "1.38"
    To copy to clipboard, switch view to plain text mode 
    (Both those numbers have an exact floating point representation)

Similar Threads

  1. Numeric rounding problems (Again)
    By vcp in forum Qt Programming
    Replies: 5
    Last Post: 5th March 2010, 18:04
  2. Problems with rounding
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2009, 23:50
  3. QDoubleSpinBox rounding
    By Talkless in forum Qt Programming
    Replies: 1
    Last Post: 7th January 2009, 12:40
  4. toDouble() and rounding problem
    By sadjoker in forum Newbie
    Replies: 10
    Last Post: 28th August 2008, 12:47
  5. Banker's rounding
    By matheww in forum General Programming
    Replies: 9
    Last Post: 25th June 2007, 22:22

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
  •  
Qt is a trademark of The Qt Company.