473,229 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,229 software developers and data experts.

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?
Jul 19 '05 #1
11 2918
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
Jul 19 '05 #2
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
Jul 19 '05 #3
Timothy Smith <ti*****@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
Jul 19 '05 #4
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.)
Jul 19 '05 #5
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
Jul 19 '05 #6
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
Jul 19 '05 #7
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
Jul 19 '05 #8
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/
Jul 19 '05 #9
>> 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

Jul 19 '05 #10

"Reinhold Birkenfeld" <re************************@wolke7.net> wrote in
message news:3g************@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).

Jul 19 '05 #11
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
Jul 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
2
by: Carl G | last post by:
I am storing a 0.000 a System.Decimal in a DataRow. On retrieval the value is only 0 without the three decimal places. It looks like the Get property returns System.Decimal.Zero, but why???? I...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
8
by: nick | last post by:
printf("%lf",3.25); the result is 3.25000 i want the answer correct to 3 decimal places What should i do? thanks!
10
by: Paul Sullivan | last post by:
decimal d; d = 1.1M OR d= (decimal) 1.1 Discussioon
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.