Mathjs: Adding different currencies without conversion

Created on 14 Dec 2017  路  5Comments  路  Source: josdejong/mathjs

Is it possible to add different currencies together in math.js WITHOUT conversion? For example, I'd like to add "(1 USD) + (1 EUR)" and have a result that keeps the two currencies distinct. Internally, this could be represented by an object: {USD: 1, EUR: 1}. Later, if I have conversion rates, I would like to be able to display the total in either USD or EUR (as shown in http://mathjs.org/examples/browser/currency_conversion.html.html). But the main thing is keeping them separated for arithmetic operations. Thanks for any input!

question

Most helpful comment

Thanks, great to hear you like mathjs :)

Yes I think that your case is too application specific to build a generic solution in mathjs itself, sorry.

I don't know the details of your application of course but when working with money, accounts, transfers I would aim for a solution where you always keep the list of transfers as the single source of truth (original data), and don't keep a sum (derived data). When there is need to calculate a sum, you can take the whole list of transfers, filter the transfers on a specific currency and calculate the sum of that. Repeat that for all different currency's and there's your total per currency. Just my 2c, maybe I'm totally misunderstanding your application ;)

All 5 comments

That sounds relatively complicated, to keep the different units separated. Maybe instead of adding them immediately, you can keep them in an array, and apply a sum to the array in the end?

Just curious: why do you want to keep them separate during calculations when you convert to a single unit in the end anyway? You can always convert from one to the other and back right?

That sounds relatively complicated, to keep the different units separated. Maybe instead of adding them immediately, you can keep them in an array, and apply a sum to the array in the end?

Thanks for the suggestion - maybe I'll do that. For the sake of elegance, I'd like to manage it in mathjs, if possible. What about imaginary numbers, they have two parts - I haven't looked at the code yet, but maybe there would be some inspiration there?

why do you want to keep them separate during calculations when you convert to a single unit in the end anyway?

This is for keeping track of multi-currency accounts, such as PayPal. The conversion is only for convenience, and the converted value will change every day depending on the exchange rates. The really important thing is the amount of each different currency.

What about imaginary numbers, they have two parts - I haven't looked at the code yet, but maybe there would be some inspiration there?

Yes, but I don't see a clear and generic use case yet to create a "special" unit which keeps the amounts of the different units as separate internal values. It's quite application specific I think.

Your PayPal example makes sense, but for such applications I think you should always keep the original exchanges in a list and do not store it as some sort of an accumulated amount per currency. You probably also have to keep the date and probably the exchange rate on that date and stuff like that for all exchanges?

I'm not sure what you mean exactly by keeping "the original exchanges in a list"? The exchange rates would be kept in a separate list... I'm working with several accounts that maintain separate sums per currency, and it had hoped to use mathjs to handle such objects. However, if it's too far outside of the design scope, I'll just take a different approach. Love mathjs, btw. Used it extensively in a project to control robotic lab experiments (https://github.com/ellis/roboliq).

Thanks, great to hear you like mathjs :)

Yes I think that your case is too application specific to build a generic solution in mathjs itself, sorry.

I don't know the details of your application of course but when working with money, accounts, transfers I would aim for a solution where you always keep the list of transfers as the single source of truth (original data), and don't keep a sum (derived data). When there is need to calculate a sum, you can take the whole list of transfers, filter the transfers on a specific currency and calculate the sum of that. Repeat that for all different currency's and there's your total per currency. Just my 2c, maybe I'm totally misunderstanding your application ;)

Was this page helpful?
0 / 5 - 0 ratings