Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > another fp problem

Reply
Thread Tools

another fp problem

 
 
Michael Hall
Guest
Posts: n/a
 
      10-06-2003
Hi All:

I'm a relative newbie to Python, apart from some CGI and shell scripting type
stuff. Anyway, I'm having some problems doing maths in Python 2.2.2 on RH9.

I'm wondering what is the best way or numerical data type to do simple monetary
calculations in Python? I only need to work with decimal numbers to two decimal
points. Are floating point numbers the only option? They're giving me hell!

If floating point numbers are the only option, how can I round off a number to
two decimal points? Converting to an integer is not working ... the cents disappear.

Leaving valuse as floating point is also not working ... I end up with figures
like $35.33000000018.

BTW, I'm putting values in and out of a MySQL database (DECIMAL data type) using
MySQLdb. The values giving me the most grief have been pulled out of the
database, where they look just fine.

Any advice graetly appreciated.

Mick

------------------------------------------
Ninti Systems: Linux Solutions and Support
Michael Hall
Mobile: 0429 095 392
Ph/Fax: 08 8953 1442
Email: office at ninti dot com
Web: http://ninti.com
------------------------------------------

 
Reply With Quote
 
 
 
 
Joerg Sonnenberger
Guest
Posts: n/a
 
      10-06-2003
On Mon, 06 Oct 2003 23:27:02 +1000, Michael Hall wrote:

> Hi All:
>
> I'm a relative newbie to Python, apart from some CGI and shell scripting type
> stuff. Anyway, I'm having some problems doing maths in Python 2.2.2 on RH9.
>
> I'm wondering what is the best way or numerical data type to do simple monetary
> calculations in Python? I only need to work with decimal numbers to two decimal
> points. Are floating point numbers the only option? They're giving me hell!


There is no monetary type yet, i.e. fixed point full precision for adds.

>
> If floating point numbers are the only option, how can I round off a number to
> two decimal points? Converting to an integer is not working ... the cents disappear.


You are asking the wrong question. The problem is 35.33 is a finite binary
fraction like 35.25 is. There a few thing you can do in python. Use normal
integer arithmetic with a power of ten as exponent i.e. the normal
scintific notation with an integer mantisse (3533 * 10**-2). Build a class
around it to implement the involved operations and provide a __float__
function for the conversion by MySQLdb.

>
> Leaving valuse as floating point is also not working ... I end up with figures
> like $35.33000000018.
>
> BTW, I'm putting values in and out of a MySQL database (DECIMAL data type) using
> MySQLdb. The values giving me the most grief have been pulled out of the
> database, where they look just fine.


Perhaps you can get them as strings? Split the string in to get the number
of digits after the decimal point and set up a class like that mentioned
above.

Joerg

>
> Any advice graetly appreciated.
>
> Mick
>
> ------------------------------------------
> Ninti Systems: Linux Solutions and Support
> Michael Hall
> Mobile: 0429 095 392
> Ph/Fax: 08 8953 1442
> Email: office at ninti dot com
> Web: http://ninti.com
> ------------------------------------------


------------ And now a word from our sponsor ------------------
Want to have instant messaging, and chat rooms, and discussion
groups for your local users or business, you need dbabble!
-- See http://netwinsite.com/sponsor/sponsor_dbabble.htm ----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to force a load of one control from another control on another form? et ASP .Net 1 06-29-2005 07:45 PM
Renaming a project with another project as the starting point? In another solution Maziar Aflatoun ASP .Net 1 01-22-2005 12:49 AM
How to access: a page from a User control, and another User controlfrom another one? qwerty ASP .Net 3 09-30-2004 05:32 PM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM
How do I write HTML tags for an applet that imports classes from another jar in another package? Ding Java 1 07-01-2004 03:41 AM



Advertisments