Velocity Reviews > How to truncate/round-off decimal numbers?

# How to truncate/round-off decimal numbers?

Girish Sahani
Guest
Posts: n/a

 06-20-2006
Hi,

I want to truncate every number to 2 digits after the decimal point. I
tried the following but it doesnt work.

>>> a = 2
>>> b = 3
>>> round(a*1.0 / b,2)

0.67000000000000004

Inspite of specifying 2 in 2nd attribute of round, it outputs all the
digits after decimal.

Sybren Stuvel
Guest
Posts: n/a

 06-20-2006
Girish Sahani enlightened us with:
> I want to truncate every number to 2 digits after the decimal point. I
> tried the following but it doesnt work.

>>>> a = 2
>>>> b = 3
>>>> round(a*1.0 / b,2)

> 0.67000000000000004

If you want to format it, use '%.2f' % (float(a)/b)

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa

MTD
Guest
Posts: n/a

 06-20-2006
> >>> a = 2
> >>> b = 3
> >>> round(a*1.0 / b,2)

> 0.67000000000000004
>
> Inspite of specifying 2 in 2nd attribute of round, it outputs all the
> digits after decimal.

This is because of floating point inaccuracy. The system cannot
accurately represent some integers, however it does its best to
approximate them. Notice this:

>>> x = 2.0/3
>>> x

0.66666666666666663
>>> round(x,2)

0.67000000000000004
>>> s = str(round(x,2))
>>> s

'0.67'

MTD
Guest
Posts: n/a

 06-20-2006
> The system cannot
> accurately represent some integers,

Er, I meant FLOATS. Doh.

Anyway, just to underline the example:

>>> x

0.66666666666666663
>>> s = str(round(x,2))
>>> s

'0.67'
>>> f = float(s)
>>> f

0.67000000000000004
>>> f == round(x,2)

True

Laurent Pointal
Guest
Posts: n/a

 06-20-2006
Girish Sahani a écrit :
> Hi,
>
> I want to truncate every number to 2 digits after the decimal point. I
> tried the following but it doesnt work.
>
>>>> a = 2
>>>> b = 3
>>>> round(a*1.0 / b,2)

> 0.67000000000000004
>
> Inspite of specifying 2 in 2nd attribute of round, it outputs all the
> digits after decimal.

There are two operations:

1) calculate of round(), which return a float number result, with the
well known problem of floating point numbers represantation (see the FAQ).

2) print that number, where default printing of last expression result
in the cli interpreter displays up to the highest precision, print
statement works differently:

>>> a=2
>>> b=3
>>> c=round(a*1.0/b,2)
>>> c

0.67000000000000004
>>> print c

0.67
>>>

A+

Laurent.

Scott David Daniels
Guest
Posts: n/a

 06-20-2006
Sybren Stuvel wrote:
> Girish Sahani enlightened us with:
>> I want to truncate every number to 2 digits after the decimal point....
>>>>> a = 2
>>>>> b = 3
>>>>> round(a*1.0 / b,2)

>> 0.67000000000000004

>
> If you want to format it, use '%.2f' % (float(a)/b)

you'll eventually discover:

>>> print round(11024. / 5000.1, 2)

only gives you "2.2", not "2.20" (which is what, I suspect, you want).

--Scott David Daniels
http://www.velocityreviews.com/forums/(E-Mail Removed)

Dan Bishop
Guest
Posts: n/a

 06-20-2006
MTD wrote:
> > The system cannot
> > accurately represent some integers,

>
> Er, I meant FLOATS. Doh.

You were also right the first time. But it only applies to integers
with more than 53 bits.

Aahz
Guest
Posts: n/a

 06-20-2006
In article <(E-Mail Removed)>,
Girish Sahani <(E-Mail Removed)> wrote:
>
>I want to truncate every number to 2 digits after the decimal point. I
>tried the following but it doesnt work.
>
>>>> a = 2
>>>> b = 3
>>>> round(a*1.0 / b,2)

>0.67000000000000004
>
>Inspite of specifying 2 in 2nd attribute of round, it outputs all the
>digits after decimal.

You should also consider switching to the decimal module if you care
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"I saw `cout' being shifted "Hello world" times to the left and stopped
right there." --Steve Gonedes

per9000
Guest
Posts: n/a

 06-21-2006
Hi,

just a thought: if you *always* work with "floats" with two decimals,
you are in fact working with integers, but you represent them as a
floats - confusing for the internal representation.

So why not work with int(float * 100) instead? This way you only have
to take care of roundoffs etc when dividing.

"int (+|-|*) int" = int
"int / int" = int / int + int % int

Integers are nice, me like integers.

/per9000

per9000
Guest
Posts: n/a

 06-21-2006
oops, should be something like this:

"int / int" = "int / int, int % int"

/per9000