Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
decimal and trunkating
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Timothy Smith  
View profile  
 More options Jun 2 2005, 5:59 am
Newsgroups: comp.lang.python
From: Timothy Smith <timo...@open-networks.net>
Date: Thu, 02 Jun 2005 19:59:08 +1000
Local: Thurs, Jun 2 2005 5:59 am
Subject: decimal and trunkating
i want to trunkate 199.999 to 199.99
getcontext.prec = 2 isn't what i'm after either, all that does is E's
the value.
do i really have to use floats to do this?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
F. Petitjean  
View profile  
 More options Jun 2 2005, 9:15 am
Newsgroups: comp.lang.python
From: "F. Petitjean" <littlejohn...@news.free.fr>
Date: 02 Jun 2005 13:15:02 GMT
Local: Thurs, Jun 2 2005 9:15 am
Subject: Re: decimal and trunkating
Le Thu, 02 Jun 2005 19:59:08 +1000, Timothy Smith a écrit :

> i want to trunkate 199.999 to 199.99

round(199.999, 2)  # 2 digits after the decimal point

> do i really have to use floats to do this?

19.999  is a float :
type(19.999) is float  #  ==> True

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Reinhold Birkenfeld  
View profile  
 More options Jun 2 2005, 9:20 am
Newsgroups: comp.lang.python
From: Reinhold Birkenfeld <reinhold-birkenfeld-nos...@wolke7.net>
Date: Thu, 02 Jun 2005 15:20:19 +0200
Local: Thurs, Jun 2 2005 9:20 am
Subject: Re: decimal and trunkating

F. Petitjean wrote:
> Le Thu, 02 Jun 2005 19:59:08 +1000, Timothy Smith a écrit :
>> i want to trunkate 199.999 to 199.99

> round(199.999, 2)  # 2 digits after the decimal point

Wrong. This will yield 200.00.

>> do i really have to use floats to do this?

> 19.999  is a float :
> type(19.999) is float  #  ==> True

He is speaking of Decimals...

d = Decimal("199.999")
d._round(5, decimal.ROUND_DOWN)

Reinhold


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc Christiansen  
View profile  
 More options Jun 2 2005, 9:14 am
Newsgroups: comp.lang.python
From: Marc Christiansen <to...@jupiter.solar-empire.de>
Date: Thu, 2 Jun 2005 15:14:29 +0200
Local: Thurs, Jun 2 2005 9:14 am
Subject: Re: decimal and trunkating

Timothy Smith <timo...@open-networks.net> wrote:
> i want to trunkate 199.999 to 199.99
> getcontext.prec = 2 isn't what i'm after either, all that does is E's
> the value.
> do i really have to use floats to do this?

You could try this (from a script I use for my phone bill):

from decimal import Decimal as d

def roundDecimal(num, prec):
    return d(num).quantize(d("1e%d" % (-prec)))

where `prec` is the number of places after the decimal point.

I'm sure there is a better solutions and someone will tell it, thereby
teaching us both. ;-)

Adiaŭ, Marc


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Hansen  
View profile  
 More options Jun 2 2005, 9:44 am
Newsgroups: comp.lang.python
From: Peter Hansen <pe...@engcorp.com>
Date: Thu, 02 Jun 2005 09:44:14 -0400
Local: Thurs, Jun 2 2005 9:44 am
Subject: Re: decimal and trunkating

Timothy Smith wrote:
> i want to trunkate 199.999 to 199.99
> getcontext.prec = 2 isn't what i'm after either, all that does is E's
> the value.
> do i really have to use floats to do this?

I think you need a context with appropriate rounding set (e.g.
ROUND_FLOOR?) and then use the quantize() method with an argument with
the appropriate number of decimal places.

For example, this works, though I'm definitely not a Decimal expert and
am confident there's a more elegant approach (which might depend on more
information about what you're doing):

 >>> d = decimal.Decimal('199.999')
 >>> decimal.getcontext().rounding = decimal.ROUND_FLOOR
 >>> d.quantize(decimal.Decimal('1.00'))
Decimal("199.99")

-Peter

(I hope this inspires someone who actually knows what he's doing with
Decimal to post an improved solution.)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Hansen  
View profile  
 More options Jun 2 2005, 9:47 am
Newsgroups: comp.lang.python
From: Peter Hansen <pe...@engcorp.com>
Date: Thu, 02 Jun 2005 09:47:54 -0400
Local: Thurs, Jun 2 2005 9:47 am
Subject: Re: decimal and trunkating

Reinhold Birkenfeld wrote:
> He is speaking of Decimals...

> d = Decimal("199.999")
> d._round(5, decimal.ROUND_DOWN)

Is one really supposed to call the underscore methods like that?

-Peter  


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Hansen  
View profile  
 More options Jun 2 2005, 9:50 am
Newsgroups: comp.lang.python
From: Peter Hansen <pe...@engcorp.com>
Date: Thu, 02 Jun 2005 09:50:21 -0400
Local: Thurs, Jun 2 2005 9:50 am
Subject: Re: decimal and trunkating

Peter Hansen wrote:
>  >>> d = decimal.Decimal('199.999')
>  >>> decimal.getcontext().rounding = decimal.ROUND_FLOOR
>  >>> d.quantize(decimal.Decimal('1.00'))
> Decimal("199.99")

Or skip changing the context and use the second argument to quantize:

d.quantize(Decimal('1.00'), decimal.ROUND_FLOOR)

-Peter


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Reinhold Birkenfeld  
View profile  
 More options Jun 2 2005, 10:49 am
Newsgroups: comp.lang.python
From: Reinhold Birkenfeld <reinhold-birkenfeld-nos...@wolke7.net>
Date: Thu, 02 Jun 2005 16:49:40 +0200
Local: Thurs, Jun 2 2005 10:49 am
Subject: Re: decimal and trunkating

Peter Hansen wrote:
> Reinhold Birkenfeld wrote:
>> He is speaking of Decimals...

>> d = Decimal("199.999")
>> d._round(5, decimal.ROUND_DOWN)

> Is one really supposed to call the underscore methods like that?

Umm... no, I think not ;) But I couldn't find something better.

Reinhold


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Facundo Batista  
View profile  
 More options Jun 2 2005, 11:57 pm
Newsgroups: comp.lang.python
From: Facundo Batista <facundobati...@gmail.com>
Date: Fri, 3 Jun 2005 00:57:40 -0300
Local: Thurs, Jun 2 2005 11:57 pm
Subject: Re: decimal and trunkating
On 6/2/05, Peter Hansen <pe...@engcorp.com> wrote:

>  >>> d = decimal.Decimal('199.999')
>  >>> decimal.getcontext().rounding = decimal.ROUND_FLOOR
>  >>> d.quantize(decimal.Decimal('1.00'))
> Decimal("199.99")

> -Peter

> (I hope this inspires someone who actually knows what he's doing with
> Decimal to post an improved solution.)

This is the right solution, but take in consideration that normally
you'll use one rounding method and you'll round to always the same
places, so, at the beggining of your program you'll do:

>>> import decimal
>>> decimal.getcontext().rounding = decimal.ROUND_FLOOR
>>> d2 = decimal.Decimal("0.01")

and each time you want to round....

>>> d = decimal.Decimal('199.999')
>>> d.quantize(d2)

Decimal("199.99")

So it's not really that ugly....

.    Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Raymond Hettinger  
View profile  
 More options Jun 3 2005, 2:34 am
Newsgroups: comp.lang.python
From: "Raymond Hettinger" <pyt...@rcn.com>
Date: 2 Jun 2005 23:34:52 -0700
Local: Fri, Jun 3 2005 2:34 am
Subject: Re: decimal and trunkating

>> i want to trunkate 199.999 to 199.99
>> getcontext.prec = 2 isn't what i'm after either, all that does
>> is E's  the value. do i really have to use floats to do this?

The precision is the total number of digits (i.e 199.99 has 5 digit
precision).  Either round to that precision level or use the quantize
method to round to a fixed number of places after the decimal point:

>>> Context(prec=5, rounding=ROUND_DOWN).create_decimal('199.999')
Decimal("199.99")
>>> Decimal('199.999').quantize(Decimal('0.01'), rounding=ROUND_DOWN)

Decimal("199.99")

Raymond Hettinger


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
chris  
View profile  
 More options Jun 3 2005, 6:08 am
Newsgroups: comp.lang.python
From: "chris" <cf_1...@hotmail.com>
Date: Fri, 03 Jun 2005 10:08:39 GMT
Local: Fri, Jun 3 2005 6:08 am
Subject: Re: decimal and trunkating

"Reinhold Birkenfeld" <reinhold-birkenfeld-nos...@wolke7.net> wrote in
message news:3g8kk4Fb5v3dU1@individual.net...

> Peter Hansen wrote:
> > Reinhold Birkenfeld wrote:
> >> He is speaking of Decimals...

> >> d = Decimal("199.999")
> >> d._round(5, decimal.ROUND_DOWN)

> > Is one really supposed to call the underscore methods like that?

> Umm... no, I think not ;) But I couldn't find something better.

> Reinhold

I'm new to Python ... and I've used decimal._round() as above. What's the
deal with using underscore methods? (A link will do if that'll save you some
typing).

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Hansen  
View profile  
 More options Jun 3 2005, 7:31 am
Newsgroups: comp.lang.python
From: Peter Hansen <pe...@engcorp.com>
Date: Fri, 03 Jun 2005 07:31:09 -0400
Local: Fri, Jun 3 2005 7:31 am
Subject: Re: decimal and trunkating

chris wrote:
> I'm new to Python ... and I've used decimal._round() as above. What's the
> deal with using underscore methods? (A link will do if that'll save you some
> typing).

Generally the underscore methods provide *internal* functionality that
might be used by other, more externally accessible (i.e. documented!)
methods in the object.  While as I've said I'm no expert in Decimal and
can't say how _round() is intended to be used, it is not documented (as
far as I can see) and certainly therefore follows this way of thinking
about underscore methods.  Several of us have found at least one
suitable alternative (i.e. quantize()) that don't rely on underscore
methods.

(Think of the underscore as being a non-binding convention that says
"don't use this externally if possible, as it doesn't form part of the
contract guaranteed by this object... it may be removed in the future,
may not work exactly as you wish, may have side effects that aren't
documented or haven't been analyzed fully when used externally, etc.")

-Peter


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Facundo Batista  
View profile  
 More options Jun 4 2005, 5:19 pm
Newsgroups: comp.lang.python
From: Facundo Batista <facundobati...@gmail.com>
Date: Sat, 4 Jun 2005 18:19:38 -0300
Local: Sat, Jun 4 2005 5:19 pm
Subject: Re: decimal and trunkating
On 2 Jun 2005 23:34:52 -0700, Raymond Hettinger <pyt...@rcn.com> wrote:

> >> i want to trunkate 199.999 to 199.99
> >> getcontext.prec = 2 isn't what i'm after either, all that does
> >> is E's  the value. do i really have to use floats to do this?

> The precision is the total number of digits (i.e 199.99 has 5 digit
> precision).  Either round to that precision level or use the quantize
> method to round to a fixed number of places after the decimal point:

God, I must stop sending mails at 2AM. I replied doing the remark that
some burden should be taken at the beggining of the program, and I
repeated that mistake, confusing everybody.

Sorry, :(

.    Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google