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?
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):
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):
> (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:
>> 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:
> 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).
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.")
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.