From 34e2d19a32f36ddef298668788447aed01c62703 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Fri, 14 Jun 2024 15:56:08 +0200 Subject: fixing and using shortFormat() --- src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt index 742bc3654..1ee34699c 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt @@ -36,19 +36,25 @@ object NumberUtil { ) ) + @Deprecated("outdated", ReplaceWith("value.shortFormat(preciseBillions)")) + fun format(value: Number, preciseBillions: Boolean = false): String = value.shortFormat(preciseBillions) + + // 1234 -> 1.2k + fun Number.shortFormat(preciseBillions: Boolean = false): String { + return compactFormat(this, preciseBillions) + } + /** * This code was modified and taken under CC BY-SA 3.0 license * @link https://stackoverflow.com/a/30661479 * @author assylias */ - - @JvmStatic - fun format(value: Number, preciseBillions: Boolean = false): String { + private fun compactFormat(value: Number, preciseBillions: Boolean = false): String { @Suppress("NAME_SHADOWING") val value = value.toLong() // Long.MIN_VALUE == -Long.MIN_VALUE, so we need an adjustment here - if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1, preciseBillions) - if (value < 0) return "-" + format(-value, preciseBillions) + if (value == Long.MIN_VALUE) return compactFormat(Long.MIN_VALUE + 1, preciseBillions) + if (value < 0) return "-" + compactFormat(-value, preciseBillions) if (value < 1000) return value.toString() // deal with small numbers -- cgit