SpinBox decimals behaviour

86 views
Skip to first unread message

Wouter Gins

unread,
Oct 20, 2014, 6:04:13 PM10/20/14
to pyqt...@googlegroups.com
Hi,

I have noticed some unusual behaviour in using the 'decimals' keyword for the spinboxes. I have some input values with a lot of digits, so I want to show all of them (let's say 8 digits). Passing along the decimals=8 keyword argument to a spinbox does not seem to display this amount of digits. Am I missing something about this keyword?

The code in the included file (shown below in text as well; not sure if Google screws up the formatting) shows a simple example. I try to show the value 123456789.987654321 in the spinbox, with decimals=10 as an option. Running this code displays 1.234567e+08 as a value, which is not exactly 10 digits, let alone 10 decimals.
Put another way: is there something I can pass along to make the spinbox show all the significant digits?


Kind regards,

Wouter Gins



import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui


class Test(object):

    def __init__(self):
        super(Test, self).__init__()

        self.app = QtGui.QApplication([])

        self.mainWin = QtGui.QMainWindow()
        self.mainWin.setWindowTitle('Test')

        self.spinbox = pg.SpinBox(value=123456789.987654321, decimals=10)

        self.mainWin.setCentralWidget(self.spinbox)

    def show(self):
        self.mainWin.show()


if __name__ == '__main__':
    import sys
    Simple = Test()
    Simple.show()
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()
SpinboxTest.py

Nicholas Tan Jerome

unread,
Oct 22, 2014, 4:42:19 AM10/22/14
to pyqt...@googlegroups.com
Hi,

in the SpinBox the Decimal value is formatted into string using the %g type, which is causing this, we can just fixed the formatting to get this working:


General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.

Anyway, I know where to change this, but using the your example, I still have slight problem with converting it into the desired value.


The problem I bumped into is this:

 
In [2]: import decimal

In [3]: decimal.Decimal(123456789.987654321)
Out[3]: Decimal('123456789.98765432834625244140625')

In [7]: float(123456789.987654321)
Out[7]: 123456789.98765433

In [11]: "{0:.10f}".format(123456789.987654321)
Out[11]: '123456789.9876543283'


It seems like there's some error in the conversion, maybe Luke knows more about this issue?
Reply all
Reply to author
Forward
0 new messages