summaryrefslogtreecommitdiff
path: root/taler-kotlin-android/src/main/java/net/taler/common
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2024-02-12 16:43:50 -0600
committerTorsten Grote <t@grobox.de>2024-03-27 14:26:40 -0300
commit1e9ee99cb07c595132fa96935e3f2b7c88dd586a (patch)
tree613ed3380a5a0cb493feb910576365f210028d3c /taler-kotlin-android/src/main/java/net/taler/common
parent39b6926dd32b0731d87ea8daa94c1d4c29d3d193 (diff)
downloadtaler-android-1e9ee99cb07c595132fa96935e3f2b7c88dd586a.tar.gz
taler-android-1e9ee99cb07c595132fa96935e3f2b7c88dd586a.tar.bz2
taler-android-1e9ee99cb07c595132fa96935e3f2b7c88dd586a.zip
[wallet] DD51: initial rendering based on currency spec
bug 0008329 (cherry picked from commit 4789103406f44ba797c7bfee3112cc6aaa228683)
Diffstat (limited to 'taler-kotlin-android/src/main/java/net/taler/common')
-rw-r--r--taler-kotlin-android/src/main/java/net/taler/common/Amount.kt46
-rw-r--r--taler-kotlin-android/src/main/java/net/taler/common/CurrencySpecification.kt36
2 files changed, 80 insertions, 2 deletions
diff --git a/taler-kotlin-android/src/main/java/net/taler/common/Amount.kt b/taler-kotlin-android/src/main/java/net/taler/common/Amount.kt
index 5fb36fa..d6188cf 100644
--- a/taler-kotlin-android/src/main/java/net/taler/common/Amount.kt
+++ b/taler-kotlin-android/src/main/java/net/taler/common/Amount.kt
@@ -16,11 +16,14 @@
package net.taler.common
+import android.os.Build
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.Serializer
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
+import java.text.DecimalFormat
+import java.text.NumberFormat
import kotlin.math.floor
import kotlin.math.pow
import kotlin.math.roundToInt
@@ -54,6 +57,11 @@ public data class Amount(
* of 50_000_000 would correspond to 50 cents.
*/
val fraction: Int,
+
+ /**
+ * Currency specification for amount
+ */
+ val spec: CurrencySpecification? = null,
) : Comparable<Amount> {
public companion object {
@@ -170,6 +178,8 @@ public data class Amount(
return Amount(checkCurrency(currency), this.value, this.fraction)
}
+ fun withSpec(spec: CurrencySpecification?) = copy(spec = spec)
+
public operator fun minus(other: Amount): Amount {
check(currency == other.currency) { "Can only subtract from same currency" }
var resultValue = value
@@ -196,8 +206,40 @@ public data class Amount(
return "$currency:$amountStr"
}
- override fun toString(): String {
- return "$amountStr $currency"
+ override fun toString() = toString(
+ showSymbol = true,
+ negative = false,
+ )
+
+ fun toString(
+ showSymbol: Boolean = true,
+ negative: Boolean = false,
+ ): String {
+ val symbols = DecimalFormat().decimalFormatSymbols
+
+ val format = if (showSymbol) {
+ NumberFormat.getCurrencyInstance()
+ } else {
+ // Make sure monetary separators are the ones used!
+ symbols.decimalSeparator = symbols.monetaryDecimalSeparator
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
+ symbols.groupingSeparator = symbols.monetaryGroupingSeparator
+ }
+
+ NumberFormat.getInstance()
+ }
+
+ if (spec != null) {
+ symbols.currencySymbol = spec.symbol(this)
+ format.maximumFractionDigits = spec.numFractionalNormalDigits
+ format.minimumFractionDigits = spec.numFractionalTrailingZeroDigits
+ } else {
+ symbols.currencySymbol = currency
+ }
+
+ (format as DecimalFormat).decimalFormatSymbols = symbols
+
+ return format.format((if (negative) "-$amountStr" else amountStr).toBigDecimal())
}
override fun compareTo(other: Amount): Int {
diff --git a/taler-kotlin-android/src/main/java/net/taler/common/CurrencySpecification.kt b/taler-kotlin-android/src/main/java/net/taler/common/CurrencySpecification.kt
new file mode 100644
index 0000000..4aa8968
--- /dev/null
+++ b/taler-kotlin-android/src/main/java/net/taler/common/CurrencySpecification.kt
@@ -0,0 +1,36 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2024 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.common
+
+import kotlinx.serialization.SerialName
+import kotlinx.serialization.Serializable
+
+@Serializable
+data class CurrencySpecification(
+ val name: String,
+ @SerialName("num_fractional_input_digits")
+ val numFractionalInputDigits: Int,
+ @SerialName("num_fractional_normal_digits")
+ val numFractionalNormalDigits: Int,
+ @SerialName("num_fractional_trailing_zero_digits")
+ val numFractionalTrailingZeroDigits: Int,
+ @SerialName("alt_unit_names")
+ val altUnitNames: Map<Int, String>,
+) {
+ // TODO: add support for alt units
+ fun symbol(amount: Amount): String = altUnitNames[0] ?: amount.currency
+} \ No newline at end of file