# [Baypiggies] rounding off problem

Jimmy Retzlaff jimmy at retzlaff.com
Wed Mar 23 17:48:06 CET 2011

```On Wed, Mar 23, 2011 at 8:34 AM, Vikram K <kpguy1975 at gmail.com> wrote:
>
> how does one round off a float number in python? i have tried this using two ways but have been unsuccessful so far:
>
> 1. IDLE 2.6.6      ==== No Subprocess ====
> >>> x = 3.5666666
> >>> x = round(x,3)
> >>> x
> 3.5670000000000002
>
> [ The above works when i try doing x = round(x,2). I am working on a windows machine. The above seems to work on the linux machine of a colleague but he gets the same problem when he tries x = round(x,4)]
>
> 2. >>> from decimal import *
> >>> getcontext().prec = 3
> >>> x = decimal(3.456654)
>
> >>> x = Decimal('3.2213333')
> >>> x
> Decimal('3.2213333')
> >>> getcontext().prec = 5
> >>> x
> Decimal('3.2213333')
> >>> y = round(x,3)
> >>> y
> 3.2210000000000001
> >>> x = 3.223344
> >>> z = round(x,3)
> >>> z
> 3.2229999999999999

Binary floating point cannot represent 3.567 exactly (just as decimal
cannot represent 1/3 exactly). Binary floating point also cannot
exactly represent 3.221 or 0.1. This is not a Python limitation or a
platform limitation, it's common to all platforms and languages that
use binary floating point (i.e., the vast majority of languages). The
print statement (actually str which is called by print) will try to
make things prettier:

>>> 3.567
3.5670000000000002
>>> print 3.567
3.567
>>> str(3.567)
'3.567'
>>> repr(3.567)
'3.5670000000000002'

You're on the right track trying to use Decimal (assuming you are
willing to trade some speed for decimal accuracy - often desirable for
financial applications), except that round converts back to binary
floating point. Use the quantize method instead:

>>> Decimal('3.2213333').quantize(Decimal('0.001'))
Decimal("3.221")
>>> str(Decimal('3.2213333').quantize(Decimal('0.001')))
'3.221'
>>> print Decimal('3.2213333').quantize(Decimal('0.001'))
3.221

For a more in depth discussion, see
http://docs.python.org/tutorial/floatingpoint.html

Jimmy
```