diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/util/FirmFormatters.kt | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/FirmFormatters.kt b/src/main/kotlin/moe/nea/firmament/util/FirmFormatters.kt index b2c1f35..96a4389 100644 --- a/src/main/kotlin/moe/nea/firmament/util/FirmFormatters.kt +++ b/src/main/kotlin/moe/nea/firmament/util/FirmFormatters.kt @@ -11,13 +11,21 @@ import kotlin.math.absoluteValue import kotlin.time.Duration object FirmFormatters { - fun toString(float: Float, fractionalDigits: Int): String = toString(float.toDouble(), fractionalDigits) - fun toString(double: Double, fractionalDigits: Int): String { + fun formatCurrency(long: Long, segments: Int = 3): String { + val α = long / 1000 + if (α != 0L) { + return formatCurrency(α, segments) + "," + (long - α * 1000).toString().padStart(3, '0') + } + return long.toString() + } + + fun formatCurrency(float: Float, fractionalDigits: Int): String = formatCurrency(float.toDouble(), fractionalDigits) + fun formatCurrency(double: Double, fractionalDigits: Int): String { val long = double.toLong() val δ = (double - long).absoluteValue val μ = pow(10, fractionalDigits) val digits = (μ * δ).toInt().toString().padStart(fractionalDigits, '0').trimEnd('0') - return long.toString() + (if (digits.isEmpty()) "" else ".$digits") + return formatCurrency(long) + (if (digits.isEmpty()) "" else ".$digits") } fun formatTimespan(duration: Duration): String { |