dylanwal / unitpy

UnitPy is a Python package for defining, converting, and working with units.
BSD 3-Clause "New" or "Revised" License
8 stars 2 forks source link

Add a currency unit #11

Open reriley opened 1 month ago

reriley commented 1 month ago

It would be very useful to have a generic "currency" unit for dealing with engineering optimization problems.

dylanwal commented 1 month ago

That’s an interesting idea! Handling currency can be challenging with so many different types and constantly fluctuating exchange rates. I’m curious about how a generic currency unit might work in practice—do you have any specific examples in mind? If you could share some coding examples of how you envision using this?

reriley commented 4 weeks ago

I agree, currency conversion is a subjective problem and difficult to implement simply. However most engineering problems do not involve multiple currencies. I'm suggesting a unit of unspecified currency to represent whatever currency is relevant to the user. I'll assume dollars for now (USD, CAD, AUD, no matter!).

A simple example would be calculations relating to power system operating costs, which often include a variable and a fixed component characterized as $ per MWh and $ per MW respectively. E.g. v = (10 * U('$/MW/h')) * (200_000 * U('MW*h')) f = (5_000 * U('$/MW')) * (10 * U('MW')) Implementation of a currency unit would avoid the need to leave the domain of UnitPy as soon as dollars are involved, and preserve the ability to check unit consistency. One could safely assume a decimal currency with a derived 'cent' unit of 100th of the base unit and cover every currency that I'm aware of in the modern age.

Although I don't think conversion is a pressing issue, there is the question of making it easily usable and which symbol to choose for the regional keyboards. There is an unspecified currency character (¤) but I think it's absent from most keyboards. The $ symbol is on US and UK querty and on French azerty keyboards at least. Perhaps a class variable could be used to allow user to change symbols but realizing this is probably outside current functionality. Or perhaps it's simply 'currency' and a helper function to convert that to a symbol for any string outputs.

Maybe I'll have a crack at submitting an implementation but interested to hear your thoughts first.