Results 1 to 2 of 2

Thread: QDoubleSpinBox rounding

  1. #1
    Join Date
    Jan 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QDoubleSpinBox rounding

    Hello,

    We have noticed anomalies in our applications accounting procedures where sale, cost prices are counted and etc. We are using Qt 4.3.5 on Windows.

    Problem looks like is in QDoubleSpinBox double rounding. Please take a look:

    Qt Code:
    1. #include <QApplication>
    2. #include <QDebug>
    3. #include <QDoubleSpinBox>
    4. #include <QWidget>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10.  
    11. QDoubleSpinBox doubleBox(&w);
    12. doubleBox.setDecimals(4);
    13.  
    14. doubleBox.setValue(2.79825);
    15. qDebug() << QString::number(doubleBox.value(),'g');
    16.  
    17. doubleBox.setValue(2.79835);
    18. qDebug() << QString::number(doubleBox.value(),'g');
    19.  
    20. w.show();
    21.  
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    You can notice that 2.79825 becomes 2.7982, and 2.79835 goes to, as I believe it really should, 2.7984.

    I have no idea what kind of "double magic" is going on. Can it be Qt bug or "natural" double rounding behavior? You also can find similar strange behavior with 2.55, 2.555, 2.5555, 2.55555 and etc. with respectively 1, 2, 3, etc. decimal places - not always you will get "..6" at the end.

    Thank you.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDoubleSpinBox rounding

    Generally double or float types are not good for money arithmetic because not all "money" values have binary represantation as double or float. The best solution is use special class for money arithmetic.
    Remember : collection of real numbers is infinite. Collection of real numbers represented by double or float type is finite.

Similar Threads

  1. toDouble() and rounding problem
    By sadjoker in forum Newbie
    Replies: 10
    Last Post: 28th August 2008, 12:47
  2. QDoubleSpinBox and QDoubleValidator
    By George Neil in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2008, 00:36
  3. QDoubleSpinBox
    By coderbob in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2008, 11:30
  4. Validator for QDoubleSpinBox
    By ehamberg in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2008, 11:37
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.