diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2024-06-14 15:56:08 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2024-06-14 15:56:08 +0200 |
commit | 34e2d19a32f36ddef298668788447aed01c62703 (patch) | |
tree | 65db564add674443a43bf65e71091e9d965f117a /src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt | |
parent | a996d8150ef2367e7c839f3565d60bdb68d2125d (diff) | |
download | skyhanni-34e2d19a32f36ddef298668788447aed01c62703.tar.gz skyhanni-34e2d19a32f36ddef298668788447aed01c62703.tar.bz2 skyhanni-34e2d19a32f36ddef298668788447aed01c62703.zip |
fixing and using shortFormat()
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt | 16 |
1 files changed, 11 insertions, 5 deletions
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 |