From aaf3c6aea823921d05ed438e727e744c5593c1ae Mon Sep 17 00:00:00 2001 From: chhoeung-mengsreang Date: Tue, 30 Nov 2021 17:01:43 +0700 Subject: [PATCH] Task: add computeFromBaseRate function in money-modules --- .../com/cubetiqs/money/MoneyExchangeUtils.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyExchangeUtils.kt b/src/main/kotlin/com/cubetiqs/money/MoneyExchangeUtils.kt index 19a7ceb..c869938 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyExchangeUtils.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyExchangeUtils.kt @@ -14,4 +14,23 @@ object MoneyExchangeUtils { fun getBaseCurrency(): StdMoney.Currency { return StdMoney.USD } + + /** + * Compute the Base Exchange Price / Converter to any Rates. + * Multiple differentiate exchange relation computation. + * Example: A -> B -> C meant You can with all variables, but must find the based MEANT of its. + * + * Sample Explanation: Matrix Multiply + * 1 USD -> 0.90 EUR + * 1 USD -> 4000 KHR + * If I want to exchange from EUR to KHR, I need. + * 1 EUR -> 1 * (1 / 0.90) / (1 / 4000) + * + * @author sombochea + * @since 1.0 + */ + fun computeFromBaseRate(amountFrom: Double = 1.0, baseRate: Double = 1.0, rateFrom: Double, rateTo: Double): Double { + // amount * ((baseRate / rateFrom) / (baseRate / rateTo)) + return amountFrom.times((baseRate.div(rateFrom)).div(baseRate.div(rateTo))) + } } \ No newline at end of file