@@ -12,28 +12,28 @@ import java.util.Currency as CurrencyCode
12
12
data class Price (val price : String? , val currency : Currency = Currency .USD ) : Comparable<Price> {
13
13
val amount: BigDecimal = price.toSanitisedBigDecimalOrZero()
14
14
15
- private val currencyFormat by lazy {
16
- val currencyFormat = DecimalFormat .getCurrencyInstance(Locale .US ) as DecimalFormat
15
+ private val currencyFormat: DecimalFormat = getCurrencyFormat()
17
16
17
+ val formattedAmount: String = when {
18
+ price.isNullOrBlank() -> " ${currency.symbol} --"
19
+ amount in belowOneThreshold -> currencyFormat.format(amount)
20
+ amount in smallThreshold -> currencyFormat.format(amount)
21
+ else -> formatLargeAmount()
22
+ }
23
+
24
+ private fun getCurrencyFormat (): DecimalFormat {
25
+ val currencyFormat = DecimalFormat .getCurrencyInstance(Locale .US ) as DecimalFormat
18
26
val decimalPlaces = if (amount in belowOneThreshold) 6 else 2
19
27
val currencyCode = try {
20
28
CurrencyCode .getInstance(currency.name)
21
29
} catch (e: IllegalArgumentException ) {
22
30
CurrencyCode .getInstance(Currency .USD .name)
23
31
}
24
-
25
32
currencyFormat.minimumFractionDigits = decimalPlaces
26
33
currencyFormat.maximumFractionDigits = decimalPlaces
27
34
currencyFormat.currency = currencyCode
28
35
29
- currencyFormat
30
- }
31
-
32
- val formattedAmount: String = when {
33
- price.isNullOrBlank() -> " ${currency.symbol} --"
34
- amount in belowOneThreshold -> currencyFormat.format(amount)
35
- amount in smallThreshold -> currencyFormat.format(amount)
36
- else -> formatLargeAmount()
36
+ return currencyFormat
37
37
}
38
38
39
39
private fun formatLargeAmount (): String {
0 commit comments