diff options
author | HiZe_ <superhize@hotmail.com> | 2023-08-10 12:23:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-10 12:23:02 +0200 |
commit | 1efe50bff3fbb0e6a782aaf5284fab3fd60ec637 (patch) | |
tree | 8ba814aee575609da3461d2be0107b8fc46f2f8b /src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt | |
parent | d0bbd687ca9d33cc7bd8f53e3103ecc92905f8dc (diff) | |
download | skyhanni-1efe50bff3fbb0e6a782aaf5284fab3fd60ec637.tar.gz skyhanni-1efe50bff3fbb0e6a782aaf5284fab3fd60ec637.tar.bz2 skyhanni-1efe50bff3fbb0e6a782aaf5284fab3fd60ec637.zip |
Merge pull request #348
* Chest Value
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt | 50 |
1 files changed, 44 insertions, 6 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt index efdb545d2..5e40a2daf 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt @@ -39,19 +39,57 @@ object NumberUtil { * @link https://stackoverflow.com/a/30661479 * @author assylias */ + @JvmStatic - fun format(value: Number): String { + fun format(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) - if (value < 0) return "-" + format(-value) + //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 < 1000) return value.toString() //deal with small numbers + val (divideBy, suffix) = suffixes.floorEntry(value) + val truncated = value / (divideBy / 10) //the number part of the output times 10 - val truncatedAt = if (suffix == "M") 1000 else 100 + + val truncatedAt = if (suffix == "M") 1000 else if (suffix == "B") 1000000 else 100 + + val hasDecimal = truncated < truncatedAt && truncated / 10.0 != (truncated / 10).toDouble() + + return if (value > 1_000_000_000 && hasDecimal && preciseBillions) { + val decimalPart = (value % 1_000_000_000) / 1_000_000 + "${truncated / 10}.$decimalPart$suffix" + } else { + if (hasDecimal) (truncated / 10.0).toString() + suffix else (truncated / 10).toString() + suffix + } + } + + @JvmStatic + fun format3(value: Number, digit: Int): 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 format3(Long.MIN_VALUE + 1, digit) + if (value < 0) return "-" + format3(-value, digit) + if (value < 1000) return value.toString() //deal with small numbers + + val (divideBy, suffix) = suffixes.floorEntry(value) + + var truncated = value / (divideBy / 10) //the number part of the output times 10 + + val truncatedAt = if (suffix == "M") 1000 else if (suffix == "B") 1000000 else 100 + val hasDecimal = truncated < truncatedAt && truncated / 10.0 != (truncated / 10).toDouble() - return if (hasDecimal) (truncated / 10.0).toString() + suffix else (truncated / 10).toString() + suffix + + // Add check for value greater than 1000000000 (1 Billion) + return if (value > 1000000000 && hasDecimal) { + val decimalPart = (value % 1000000000) / 1000000 // Extract 3 digits after the decimal point + "${(truncated / 10).toDouble().toString().take(digit + 2)}$suffix" + } else { + if (hasDecimal) (truncated / 10.0).toString().take(digit + 2) + suffix else (truncated / 10).toString() + suffix + } } /** |