473,795 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2976
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.99 9")
d._round(5, decimal.ROUND_D OWN)

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(nu m, 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.getcont ext().rounding = decimal.ROUND_F LOOR
d.quantize(deci mal.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.99 9")
d._round(5, decimal.ROUND_D OWN)


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.getcont ext().rounding = decimal.ROUND_F LOOR
>>> d.quantize(deci mal.Decimal('1. 00'))

Decimal("199.99 ")


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

d.quantize(Deci mal('1.00'), decimal.ROUND_F LOOR)

-Peter
Jul 19 '05 #7
Peter Hansen wrote:
Reinhold Birkenfeld wrote:
He is speaking of Decimals...

d = Decimal("199.99 9")
d._round(5, decimal.ROUND_D OWN)


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.getcont ext().rounding = decimal.ROUND_F LOOR
>>> d.quantize(deci mal.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.getcont ext().rounding = decimal.ROUND_F LOOR
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_de cimal('199.999' ) Decimal("199.99 ") Decimal('199.99 9').quantize(De cimal('0.01'), rounding=ROUND_ DOWN)

Decimal("199.99 ")
Raymond Hettinger

Jul 19 '05 #10

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

Similar topics

21
4539
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
6154
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
2
3605
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 can't figure out why the design is so that the DataRow "alters" the value entered. In my application a decimal column in a row of a specific table has a fix number of decimal places according to certain premises. The
2
3469
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 with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
8
11331
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
8153
by: Paul Sullivan | last post by:
decimal d; d = 1.1M OR d= (decimal) 1.1 Discussioon
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.