Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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.
 
Reply With Quote
 
 
 
 
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.


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
 
Reply With Quote
 
 
 
 
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'

 
Reply With Quote
 
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

 
Reply With Quote
 
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.
 
Reply With Quote
 
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)


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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
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.

 
Reply With Quote
 
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
about getting correct results.
--
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
 
Reply With Quote
 
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

 
Reply With Quote
 
per9000
Guest
Posts: n/a
 
      06-21-2006
oops, should be something like this:

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

/per9000

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to convert from Decimal('1.23456789') to Decimal('1.234') valpa Python 11 03-24-2009 07:11 AM
Error: Cannot convert Decimal("0.0000") to Decimal Vitaliy Python 1 05-29-2008 10:36 AM
TypeError: unsupported operand type(s) for -: 'Decimal' and 'Decimal'. Why? Gilbert Fine Python 8 08-01-2007 01:58 AM
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM



Advertisments