Python Forum
Error in Python 3.6 and how to limit decimal places - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Error in Python 3.6 and how to limit decimal places (/thread-2452.html)



Error in Python 3.6 and how to limit decimal places - Raptor88 - Mar-17-2017

Using Python 3.6 and tkinter that comes with it.

My tkinter code:
for myvalue in range(2,11):
    Radiobutton(rightFrame, text=myvalue*.1, font=(None, 14), variable=var, value=myvalue).pack()
Produces radio buttons with the following values:
o 0.2
o 0.30000000000000004
o 0.4
o 0.5
o 0.60000000000000001
o 0.70000000000000001
o 0.8
o 0.9
o 1.0

Why does Python 3.6 erroneously calculate the .3, .6 and .7 values?
And more importantly, what's the simplest way to limit the output to one decimal place using my code?


RE: Error in Python 3.6 and how to limit decimal places - metulburr - Mar-17-2017

Quote:what's the simplest way to limit the output to one decimal place using my code?

utilize the format method
>>> value = 0.60000000000000001
>>> '{:.1}'.format(value)
'0.6'



RE: Error in Python 3.6 and how to limit decimal places - ichabod801 - Mar-17-2017

(Mar-17-2017, 09:24 PM)Raptor88 Wrote: Why does Python 3.6 erroneously calculate the .3, .6 and .7 values?

It's called floating point arithmetic. The numbers aren't stored as decimals, they're stored as the fraction of two binary integers. For some decimals they can get close, but not quite there.


RE: Error in Python 3.6 and how to limit decimal places - snippsat - Mar-17-2017

(Mar-17-2017, 09:24 PM)Raptor88 Wrote: Why does Python 3.6 erroneously calculate the .3, .6 and .7 values?
It's the way floating point arithmetic work,Basic Answers Python doc.
(Mar-17-2017, 09:31 PM)Raptor88 Wrote: Using Python 3.6
36 has also new f-string.
>>> value = 0.30000000000000004
>>> f'{value:.1}'
'0.3'
There is also a Decimal module.
>>> 0.4 - 0.1
0.30000000000000004

>>> from decimal import Decimal
>>> Decimal('0.4') - Decimal('0.1')
Decimal('0.3')



RE: Error in Python 3.6 and how to limit decimal places - Raptor88 - Mar-17-2017

metulburr, ichabod801 and snippsat,
    Thanks for your responses.

I tried the suggestions from metulburr and snippsat:
# metulburr:
for myvalue in range(2,11):
    Radiobutton(rightFrame, text='{:.1}'.format(myvalue*.1), font=(None, 14), variable=var, value=myvalue).pack()

#snippsat:
for myvalue in range(2,11):
    Radiobutton(rightFrame, text=f'{myvalue*.1:.1}', font=(None, 14), variable=var, value=myvalue).pack()
But they both result in the following output:
o 0.2
o 0.3
o 0.4
o 0.5
o 0.6
o 0.7
o 0.8
o 0.9
o 1e+00

How can I get 1.0 for the last radio widget using your code?

I could use the for loop for values 0.2 to 0.9 and a separate statement for the last 1.0 radio widget.  But interested in how to resolve the problem using your code.


RE: Error in Python 3.6 and how to limit decimal places - snippsat - Mar-17-2017

Add f.
>>> value = 1e+00
>>> f'{value:.1}'
'1e+00'

# f
>>> f'{value:.1f}'
'1.0'
>>> '{:.1f}'.format(value)
'1.0'



RE: Error in Python 3.6 and how to limit decimal places - Raptor88 - Mar-18-2017

(Mar-17-2017, 10:54 PM)snippsat Wrote: Add f.
..... snip .....

snippsat,

Adding f produces perfect output.  For other beginners, here's the code:
for myvalue in range(2,11):
    Radiobutton(rightFrame, text=f'{myvalue*.1:.1f}', font=(None, 14), variable=var, value=myvalue).pack()
.
.
Using your other suggestion, I like the following code better since it's easier to understand and also produces perfect output:
for myvalue in range(2,11):
    Radiobutton(rightFrame, text=myvalue*Decimal('.1'), font=(None, 14), variable=var, value=myvalue).pack()
Thanks for your help,
Raptor88