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..6dc9ce249 100644 --- a/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt +++ b/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt @@ -13,6 +13,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 +48,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 {