Updated on 2026-08-14
This commit is contained in:
commit
cab30e9431
186 changed files with 3729 additions and 2030 deletions
|
|
@ -1,43 +0,0 @@
|
|||
package com.tangem.utils
|
||||
|
||||
import java.math.BigDecimal
|
||||
import java.math.RoundingMode
|
||||
import java.text.DecimalFormat
|
||||
import java.text.NumberFormat
|
||||
import java.util.Locale
|
||||
|
||||
// todo determine where to place this extensions
|
||||
fun BigDecimal.toFormattedString(
|
||||
decimals: Int,
|
||||
roundingMode: RoundingMode = RoundingMode.DOWN,
|
||||
locale: Locale = Locale.getDefault(),
|
||||
): String {
|
||||
val formatter = NumberFormat.getInstance(locale) as? DecimalFormat
|
||||
val df = formatter?.apply {
|
||||
maximumFractionDigits = decimals
|
||||
minimumFractionDigits = 0
|
||||
isGroupingUsed = true
|
||||
this.roundingMode = roundingMode
|
||||
}
|
||||
return df?.format(this) ?: this.toPlainString()
|
||||
}
|
||||
|
||||
@Suppress("MagicNumber")
|
||||
fun BigDecimal.toFormattedCurrencyString(
|
||||
decimals: Int,
|
||||
currency: String? = null,
|
||||
roundingMode: RoundingMode = RoundingMode.DOWN,
|
||||
limitNumberOfDecimals: Boolean = true,
|
||||
): String {
|
||||
val decimalsForRounding = if (limitNumberOfDecimals) {
|
||||
if (decimals > 8) 8 else decimals
|
||||
} else {
|
||||
decimals
|
||||
}
|
||||
val formattedAmount = this.toFormattedString(
|
||||
decimals = decimalsForRounding,
|
||||
roundingMode = roundingMode,
|
||||
)
|
||||
val formattedCurrency = currency?.let { " $it" } ?: ""
|
||||
return "$formattedAmount$formattedCurrency"
|
||||
}
|
||||
|
|
@ -11,4 +11,19 @@ interface Converter<I : Any, O : Any?> {
|
|||
fun convertSet(input: Collection<I>): Set<O> {
|
||||
return input.mapTo(hashSetOf(), ::convert)
|
||||
}
|
||||
|
||||
fun convertListIgnoreErrors(input: Collection<I>, onError: ((Throwable) -> Unit)? = null): List<O> {
|
||||
return input.mapNotNull {
|
||||
try {
|
||||
convert(it)
|
||||
} catch (throwable: Throwable) {
|
||||
onError?.invoke(throwable)
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> T?.asMandatory(name: String): T {
|
||||
return this ?: error("$name must not be null")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue