Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How would one handle arithmetics across different currencies? #29

Open
valeeum opened this issue Jun 30, 2017 · 2 comments
Open

How would one handle arithmetics across different currencies? #29

valeeum opened this issue Jun 30, 2017 · 2 comments

Comments

@valeeum
Copy link

valeeum commented Jun 30, 2017

Is there a built-in mechanism for currency conversion arithmetics?

@ofumbi
Copy link

ofumbi commented Jul 18, 2017

src_amount = '2306.56' // USD you need to convert
var ratio = 1.23 ; // exchange ratio USD=>EUR
var src = Money['USD'];
var src_len = src.decimal_digits;
var dst = Money['EUR'];
var dst_len = dst.decimal_digits;
var subunitDifference = src_len - dst_len;
ratio = ratio / Math.pow(10, subunitDifference);
var src_amt = Money.fromDecimal(src_amount, 'USD' , Math.ceil);
var counterValue = src_amt.multiply(ratio, Math.ceil);
var dst_amt = new Money(counterValue.getAmount(), 'EUR');

@davidkalosi
Copy link
Owner

maybe we could add a CurrencyConverter implementation that would expose a method like this
var currencyConverter = new CurrencyConverter(); var target = currencyConverter.convert(source, ratio);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants