From 0676972a839ee970a25428999ffaf53e7ad96045 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Tue, 9 Feb 2021 12:27:31 +0700 Subject: [PATCH] Add find currency from java built-in and fixed for money view --- src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt | 7 +++++++ src/main/kotlin/com/cubetiqs/money/MoneyView.kt | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt b/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt index 67e9e14..9fc5c95 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt @@ -1,5 +1,7 @@ package com.cubetiqs.money +import java.util.* + infix fun StdMoney.exchangeTo(currency: StdMoney.Currency): StdMoney { return MoneyExchangeUtils.exchange(this, currency) } @@ -126,4 +128,9 @@ fun MoneyView.asStdMoney(): StdMoney { return this@asStdMoney.getValue() } } +} + +// detect currency symbol, if needed +fun StdMoney.Currency.findCurrency(): Currency? { + return Currency.getInstance(this.getCurrency()) } \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyView.kt b/src/main/kotlin/com/cubetiqs/money/MoneyView.kt index 3762a79..0e8b554 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyView.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyView.kt @@ -4,9 +4,12 @@ open class MoneyView( private var value: Number? = null, private var currency: String? = null, ) : MoneyMixin { + private var _currency: StdMoney.Currency? = null + constructor(money: StdMoney) : this() { + this._currency = money.getCurrency() this.value = money.getValue() - this.currency = money.getCurrency().getCurrency() + this.currency = this._currency?.getCurrency() } fun getValue(): Double { @@ -17,6 +20,10 @@ open class MoneyView( return currency } + fun getSymbol(): String? { + return this._currency?.findCurrency()?.symbol + } + fun getFormat(): String { return MoneyConfig .getFormatter(getCurrency())