From 945cd2af2c8a477486ef232fc8a78465b1319648 Mon Sep 17 00:00:00 2001 From: Naveen Singh Date: Sun, 8 Feb 2026 16:48:50 +0530 Subject: [PATCH 1/2] fix: use SI decimal file size units Refs: https://github.com/FossifyOrg/General-Discussion/issues/794 --- .../kotlin/org/fossify/commons/extensions/Int.kt | 12 +++++++----- .../kotlin/org/fossify/commons/extensions/Long.kt | 11 +++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt b/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt index b374173a8..758845b27 100644 --- a/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt +++ b/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt @@ -6,6 +6,7 @@ import android.graphics.Color import android.media.ExifInterface import android.os.Handler import android.os.Looper +import android.text.format.Formatter import androidx.core.graphics.ColorUtils import androidx.core.os.postDelayed import org.fossify.commons.helpers.DARK_GREY @@ -13,6 +14,8 @@ import org.fossify.commons.helpers.WCAG_AA_NORMAL import java.text.DecimalFormat import java.util.Locale import java.util.Random +import kotlin.math.log10 +import kotlin.math.pow fun Int.getContrastColor(): Int { val luminance = ColorUtils.calculateLuminance(this) @@ -46,14 +49,13 @@ fun Int.getFormattedDuration(forceShowHours: Boolean = false): String { return sb.toString() } +@Deprecated("Use Long.formatSize() instead.") fun Int.formatSize(): String { - if (this <= 0) { - return "0 B" - } + if (this <= 0) return "0 B" val units = arrayOf("B", "kB", "MB", "GB", "TB") - val digitGroups = (Math.log10(toDouble()) / Math.log10(1024.0)).toInt() - return "${DecimalFormat("#,##0.#").format(this / Math.pow(1024.0, digitGroups.toDouble()))} ${units[digitGroups]}" + val digitGroups = (log10(toDouble()) / log10(1000.0)).toInt() + return "${DecimalFormat("#,##0.#").format(this / 1000.0.pow(digitGroups.toDouble()))} ${units[digitGroups]}" } @Deprecated( diff --git a/commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt b/commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt index a21fdccce..5a4a11a54 100644 --- a/commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt +++ b/commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt @@ -7,16 +7,15 @@ import android.text.format.Time import java.text.DecimalFormat import java.util.Calendar import java.util.Locale +import kotlin.math.log10 +import kotlin.math.pow fun Long.formatSize(): String { - // https://stackoverflow.com/a/5599842 - if (this <= 0) { - return "0 B" - } + if (this <= 0) return "0 B" val units = arrayOf("B", "kB", "MB", "GB", "TB", "PB", "EB") - val digitGroups = (Math.log10(toDouble()) / Math.log10(1024.0)).toInt() - return "${DecimalFormat("#,##0.#").format(this / Math.pow(1024.0, digitGroups.toDouble()))} ${units[digitGroups]}" + val digitGroups = (log10(toDouble()) / log10(1000.0)).toInt() + return "${DecimalFormat("#,##0.#").format(this / 1000.0.pow(digitGroups.toDouble()))} ${units[digitGroups]}" } fun Long.formatDate(context: Context, dateFormat: String? = null, timeFormat: String? = null): String { From 2734522113344dd079f0deb0ecc52ca2a18c73f0 Mon Sep 17 00:00:00 2001 From: Naveen Singh Date: Sun, 8 Feb 2026 16:54:05 +0530 Subject: [PATCH 2/2] refactor: optimize imports --- commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt b/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt index 758845b27..6dc9ce249 100644 --- a/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt +++ b/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt @@ -6,7 +6,6 @@ import android.graphics.Color import android.media.ExifInterface import android.os.Handler import android.os.Looper -import android.text.format.Formatter import androidx.core.graphics.ColorUtils import androidx.core.os.postDelayed import org.fossify.commons.helpers.DARK_GREY