ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-Tutor
python-Tutor
Re: [Tutor] round() yields in-expected results/commercial rounding
by Michael Janssen other posts by this author
Dec 12 2003 10:21AM messages near this date
Re: [Tutor] round() yields in-expected results/commercial rounding | [Tutor] Re: Reuse of callback
On Fri, 12 Dec 2003 Harm_Kirchhoff@[...].jp wrote:

>  I have the following problem:
> 
>  >>> float(8.7)
>  8.6999999999999993
>  >>> float(870)/100
>  8.6999999999999993
>  >>> 8.7 * 100
>  869.99999999999989
>  >>> round(8.7,2)
>  8.6999999999999993
>  >>> math.modf(8.7)
>  (0.69999999999999929, 8.0)
>  >>> int(float(8.7*100))
>  869

This is what int is suppose to do. As lib/built-in-funcs.html states:
"Conversion of floating point numbers to integers truncates (towards
zero)."

better:

[user@host]# wget http://starship.python.net/crew/aahz/FixedPoint.py

[user@host]# python

> >> import FixedPoint
> >> dir(FixedPoint)
['DEFAULT_PRECISION', 'FixedPoint', '__builtins__', '__doc__',
'__file__', '__name__', '__version__', '_mkFP', '_norm', '_parser',
'_roundquotient', '_string2exact', '_tento', '_test']
> >> f = FixedPoint.FixedPoint(8.7)
> >> f
FixedPoint('8.70', 2)
> >> f * 100
FixedPoint('870.00', 2)
> >> int(f * 100)
870

--->  a simple programming-language arithmetic isn't good enough for
Money Application. You will loose some money when you use it (or
gain some ;-).

So you should look out for solutions made for this problem (google for
"python+money"). FixedPoint seems for me like a very good starting
point to write your programm correctly. Perhaps FixedPoint offers
allready everything you need.

But you might also want support different types of commercial
rounding conventions (Shall $4.225 be rounded up or down?). I've heard
some people work on a Money class to solve exactly these type of things
but it is still in progress. There was a python-dev thread not long ago:
http://mail.python.org/pipermail/python-dev/2003-October/038902.html

This was a "prePEP" (which stands for Python Enhancemeent Proposal) as
you notice but it (and the further discussion) might give you some very
valueable informations.


Michael








_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:

Daniel Ehrenberg
Michael Janssen

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved