diff options
author | hannibal2 <24389977+hannibal002@users.noreply.github.com> | 2024-03-01 11:58:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-01 11:58:25 +0100 |
commit | 54131e52b01829f5eedf89f2a1132139c7513f68 (patch) | |
tree | 7ea9abcabbd5e1a9c72166214c13ac2ab6bc4103 /src/main | |
parent | 0884b9a12d51d588d8cc46a295d9488054110731 (diff) | |
download | skyhanni-54131e52b01829f5eedf89f2a1132139c7513f68.tar.gz skyhanni-54131e52b01829f5eedf89f2a1132139c7513f68.tar.bz2 skyhanni-54131e52b01829f5eedf89f2a1132139c7513f68.zip |
Internal Change: String to number final changes (#1078)
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt | 27 |
1 files changed, 22 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 b481eff35..c4dea1046 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt @@ -90,7 +90,7 @@ object NumberUtil { fun Number.ordinal(): String { val long = this.toLong() - if (long % 100 in 11 .. 13) return "th" + if (long % 100 in 11..13) return "th" return when (long % 10) { 1L -> "st" 2L -> "nd" @@ -189,16 +189,33 @@ object NumberUtil { // TODO create new function formatLong, and eventually deprecate this function. @Deprecated("renamed", ReplaceWith("this.formatLong()")) - fun String.formatNumber(): Long = formatLong() ?: throw NumberFormatException("formatNumber has a NumberFormatException with '$this'") + fun String.formatNumber(): Long = formatLong() - fun String.formatLong(): Long? = formatDouble()?.toLong() + fun String.formatDouble(): Double = + formatDoubleOrNull() ?: throw NumberFormatException("formatDouble failed for '$this'") - fun String.formatLongOrUserError(): Long? = formatDouble()?.toLong() ?: run { + fun String.formatLong(): Long = + formatDoubleOrNull()?.toLong() ?: throw NumberFormatException("formatLong failed for '$this'") + + fun String.formatInt(): Int = + formatDoubleOrNull()?.toInt() ?: throw NumberFormatException("formatInt failed for '$this'") + + fun String.formatDoubleOrUserError(): Double? = formatDoubleOrNull() ?: run { + ChatUtils.userError("Not a valid number: '$this'") + return@run null + } + + fun String.formatLongOrUserError(): Long? = formatDoubleOrNull()?.toLong() ?: run { + ChatUtils.userError("Not a valid number: '$this'") + return@run null + } + + fun String.formatIntOrUserError(): Int? = formatDoubleOrNull()?.toInt() ?: run { ChatUtils.userError("Not a valid number: '$this'") return@run null } - fun String.formatDouble(): Double? { + private fun String.formatDoubleOrNull(): Double? { var text = lowercase().replace(",", "") val multiplier = if (text.endsWith("k")) { |