best way of dealing with currency?

P

Paul Miller

I'm looking at doing some currency calculations in some Python code
integrated with a C++ application. I want to be able to come up with the
same values I get in an Excel spreadsheet.

I've been poking around for a couple of days and haven't come across a
definitive method for dealing with currency in a precise manner.

What do I need to do? Use rationals? Very high-precision floating-point?
mxNumber? Or something else?

Thanks for any and all input!
 
D

Diez B. Roggisch

Paul said:
I'm looking at doing some currency calculations in some Python code
integrated with a C++ application. I want to be able to come up with the
same values I get in an Excel spreadsheet.

I've been poking around for a couple of days and haven't come across a
definitive method for dealing with currency in a precise manner.

What do I need to do? Use rationals? Very high-precision floating-point?
mxNumber? Or something else?

The module decimal. But in the end, you might not be able to recreate
the same values as excel, due to the unknown intricacies of excel.

Diez
 
M

Mark Shroyer

The module decimal. But in the end, you might not be able to recreate
the same values as excel, due to the unknown intricacies of excel.

But it can be done! All it takes is a little dedication. Here's a
function that the OP might find useful, for example:

def excel_multiply(a, b):
if a * b != 850 * 77.1:
return a * b
else:
return 100000

Best of luck,
Mark
 
A

Asun Friere

But it can be done! All it takes is a little dedication. Here's a
function that the OP might find useful, for example:

def excel_multiply(a, b):
if a * b != 850 * 77.1:
return a * b
else:
return 100000

Best of luck,
Mark

OK joke, but it's worth noting that python's decimal behaves in the
same way as Excel given this example:
 
P

Paddy

I'm looking at doing some currency calculations in some Python code
integrated with a C++ application. I want to be able to come up with the
same values I get in an Excel spreadsheet.

Ouch! do really want to come up with the Excel result? Sometimes
people have made expensive mistakes when doing financial calculations
in Excel. Part of the problem is Excels calculation errors, and part
is due to spreadsheets being inherently hard to review, and easy to
silently change.

I'd suggest you use Pythons decimal arithmetic module for precise
calculations (i.e. to a precision that you specify), and don't blindly
assume the source of any discrepancies but investigate them
impartially.

(Here's a link to some alarming statistics from a HM customs oficial:
http://www.financeweek.co.uk/cgi-bin/item.cgi?id=5120&d=302&h=24&f=254
).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top