Answer the question
In order to leave comments, you need to log in
Rounding numbers. What am I doing wrong?
Python 3.4
I have this code:
f2 = round(f, 3)
buffer = 0.001
if f2 < buffer:
r = {'f=':f,'round(f,3)=':f2,'buffer=':buffer}
{'f=': 3.564236791125941e-09, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 1.3266317874845862e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 1.9855396039131223e-08, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 6.35513772582379e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 1.7587640969915497e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 2.832111881297117e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 2.5486645882828902e-05, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 8.51481378687756e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 3.240056853934595e-07, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 2.5071613169100337e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 4.280439648979284e-07, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 4.778466944528603e-07, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 2.841350227730452e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 4.24937996249021e-05, 'buffer=': 0.001, 'round(f,3)=': 0.0}
if
else:
r = 0
f
to f2
? Answer the question
In order to leave comments, you need to log in
>>> Decimal(0.0005)
Out[23]: Decimal('0.0005000000000000000104083408558608425664715468883514404296875')
>>> Decimal('0.0005')
Out[24]: Decimal('0.0005')
>>> round(Decimal('0.0005'), 3)
Out[25]: Decimal('0.000')
>>> round(Decimal(0.0005), 3)
Out[26]: Decimal('0.001')
>>> round(Decimal(0.0006), 3)
Out[27]: Decimal('0.001')
>>> round(Decimal(0.0004), 3)
Out[28]: Decimal('0.000')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question