Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 5 additions & 6 deletions commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading