Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

How to truncate/round-off decimal numbers?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


girish at cse

Jun 20, 2006, 2:14 AM

Post #1 of 14 (993 views)
Permalink
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.
--
http://mail.python.org/mailman/listinfo/python-list


marc.t.davies at gmail

Jun 20, 2006, 2:24 AM

Post #2 of 14 (977 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

> >>> 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'

--
http://mail.python.org/mailman/listinfo/python-list


marc.t.davies at gmail

Jun 20, 2006, 2:26 AM

Post #3 of 14 (976 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

> 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

--
http://mail.python.org/mailman/listinfo/python-list


laurent.pointal at limsi

Jun 20, 2006, 2:29 AM

Post #4 of 14 (972 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

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.
--
http://mail.python.org/mailman/listinfo/python-list


scott.daniels at acm

Jun 20, 2006, 9:03 AM

Post #5 of 14 (974 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

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 [at] acm
--
http://mail.python.org/mailman/listinfo/python-list


danb_83 at yahoo

Jun 20, 2006, 9:09 AM

Post #6 of 14 (972 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

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.

--
http://mail.python.org/mailman/listinfo/python-list


aahz at pythoncraft

Jun 20, 2006, 9:20 AM

Post #7 of 14 (980 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

In article <mailman.7238.1150794927.27775.python-list [at] python>,
Girish Sahani <girish [at] cse> 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 [at] pythoncraft) <*> http://www.pythoncraft.com/

"I saw `cout' being shifted "Hello world" times to the left and stopped
right there." --Steve Gonedes
--
http://mail.python.org/mailman/listinfo/python-list


per9000 at gmail

Jun 21, 2006, 12:44 AM

Post #8 of 14 (972 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

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

--
http://mail.python.org/mailman/listinfo/python-list


per9000 at gmail

Jun 21, 2006, 12:48 AM

Post #9 of 14 (972 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

oops, should be something like this:

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

/per9000

--
http://mail.python.org/mailman/listinfo/python-list


jmbc at nospam

Jun 21, 2006, 1:33 AM

Post #10 of 14 (977 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

> 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.
And why won't you work with decimal module ?
--
http://mail.python.org/mailman/listinfo/python-list


nmm1 at cus

Jun 21, 2006, 1:57 AM

Post #11 of 14 (976 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

In article <1150875897.866447.312150 [at] g10g2000cwb>,
"per9000" <per9000 [at] gmail> writes:
|>
|> 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.

No, you aren't - you are working with fixed-point, which is something
that is neither integers nor floating-point, but is somewhere in
between. I am (just) old enough to remember when it was used for
numeric work, and to have used it for that myself, but not old enough
to have done any numeric work using fixed-point hardware.

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

And multiplying, and calling most mathematical functions.


Regards,
Nick Maclaren.
--
http://mail.python.org/mailman/listinfo/python-list


per9000 at gmail

Jun 21, 2006, 4:20 AM

Post #12 of 14 (986 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

Nick Maclaren wrote:
> |> 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.
>
> No, you aren't - you are working with fixed-point

Nick, your answer has so many layers, I'll try to explain how I think
:-D

1) if you use integers you can think of them as having one part bigger
than 100 and one part smaller than 100, like so:
>>> a = 11122
>>> (a/100,a%100)
(111, 22)
Here the output 111,22 looks like something else than an integer, but
this is just a matter of representation. a *is* an integer, but we
represent it *as if it was* a "decimal" number. (Compare with
(minutes,seconds) or (euro,cents) or (feet,inch) or any other
"arbitrary" position system)

2) If we use floats with two decimals
>>> b = 222.33
>>> b
222.33000000000001
they look like fix-point numbers (I had to look it up
http://en.wikipedia.org/wiki/Fixed-point :-D) but python stores it
(correct me if I am wrong) as a float (or double or quad or whatever).
If we want to work with fix-point aritmetics we have to invent new
functions to do most math.

3) Most "decimal numbers" cannot be stored exactly as floats - that is
why b gave the ugly print. But some can, f.x
>>> quart = 0.25
>>> quart
0.25
quart translates to a finite "decimal" number in binary (0.01 I think).

The point is: representing should-be integers as floats makes you loose
precision (negligable probalby but still...).

4)
> |> So why not work with int(float * 100) instead? This way you only have
> |> to take care of roundoffs etc when dividing.
>
> And multiplying, and calling most mathematical functions.
You are correct of course. My mistake.

But, the multiplication is exact before you start rounding off - I wont
start counting ordos for int*int vs. float*float, but it could have
some advantages
>>> a
11122
>>> b
22233
>>> a*b
247275426
>>> (a*b/10000,a*b%10000)
(24727, 5426)
On the other hand you will quickly loose accuracy if you perform
multiple multiplications or divisions or use other mathematical
functions.

5) So, when could this way of thinking be useful? Well, rarely, but if
you only add/subtract "decimals" and/or multiply "decimals" with whole
numbers or if you want to use some non-metric system to do scientific
stuff (compute square feet when having (feet1,inch1) * (feet2,inch2)
assuming that inches are atomic.)

This could of course be extended to
(feet, inch, quarter_of_afoot, sixteeth_of_a_foot) if you'd like - it
is all a matter of representation.

Regards,
Per
----
"It is a gift. A gift to the foes of 'the Terrorists'. Why not
use this 'terrorism'? Long has my father, 'George Bush Sr',
kept the forces of 'the terrorists' at bay. By the blood of
our people are your lands kept safe. Give 'the land of the
brave' the weapon of the enemy. Let us use it against him."

--
http://mail.python.org/mailman/listinfo/python-list


scott.daniels at acm

Jun 21, 2006, 10:36 AM

Post #13 of 14 (975 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

Nick Maclaren wrote: (of fixed point)
> .... I am (just) old enough to remember when it was used for
> numeric work, and to have used it for that myself, but not old enough
> to have done any numeric work using fixed-point hardware.

You are using fixed point hardware today. Fixed point tracked the
"decimal point" (or "binary point" or whatever) in the mind of the
programmer, not in the state of the hardware. There never was
"fixed point hardware," it was simply a way of viewing the results
of the gates of the same hardware we use today.

--
--Scott David Daniels
scott.daniels [at] acm
--
http://mail.python.org/mailman/listinfo/python-list


nmm1 at cus

Jun 21, 2006, 10:45 AM

Post #14 of 14 (977 views)
Permalink
Re: How to truncate/round-off decimal numbers? [In reply to]

In article <44997ea8$1 [at] nntp0>,
Scott David Daniels <scott.daniels [at] acm> writes:
|> Nick Maclaren wrote: (of fixed point)
|> > .... I am (just) old enough to remember when it was used for
|> > numeric work, and to have used it for that myself, but not old enough
|> > to have done any numeric work using fixed-point hardware.
|>
|> You are using fixed point hardware today. Fixed point tracked the
|> "decimal point" (or "binary point" or whatever) in the mind of the
|> programmer, not in the state of the hardware. There never was
|> "fixed point hardware," it was simply a way of viewing the results
|> of the gates of the same hardware we use today.

Er, no. Analogue hardware never was fixed-point, and I have used that :-)

More relevantly, you are using a very parochial and unusual interpretation
of the word 'hardware'. In normal usage, it includes everything below
the instruct set specification (now often called the ABI). And I can
assure you that there is a considerable difference between the hardware
that provides a floating-point interface at the ABI and that which
provides a fixed-point interface.


Regards,
Nick Maclaren.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.