Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to truncate/round-off decimal numbers? (http://www.velocityreviews.com/forums/t358839-how-to-truncate-round-off-decimal-numbers.html)

 Girish Sahani 06-20-2006 09:14 AM

How to truncate/round-off decimal numbers?

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 06-20-2006 09:22 AM

Re: How to truncate/round-off decimal numbers?

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.

Yes it does, read the documentation about floating points.

>>>> 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 06-20-2006 09:24 AM

Re: How to truncate/round-off decimal numbers?

> >>> 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 06-20-2006 09:26 AM

Re: How to truncate/round-off decimal numbers?

> 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 06-20-2006 09:29 AM

Re: How to truncate/round-off decimal numbers?

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 06-20-2006 04:03 PM

Re: How to truncate/round-off decimal numbers?

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)

Sybren has this right. If you follow everyone else's advice,
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
scott.daniels@acm.org

 Dan Bishop 06-20-2006 04:09 PM

Re: How to truncate/round-off decimal numbers?

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 06-20-2006 04:20 PM

Re: How to truncate/round-off decimal numbers?

In article <mailman.7238.1150794927.27775.python-list@python.org>,
Girish Sahani <girish@cse.iitb.ac.in> 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
about getting correct results.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

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

 per9000 06-21-2006 07:44 AM

Re: How to truncate/round-off decimal numbers?

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 06-21-2006 07:48 AM

Re: How to truncate/round-off decimal numbers?

oops, should be something like this:

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

/per9000

All times are GMT. The time now is 09:38 AM.