arblant
FoxyVPN-MultiAcc / app / src / main / java / com / vauth / foxyvpn / data / ByteFormat.kt
ByteFormat.kt22 lines · 650 B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
package com.vauth.foxyvpn.data

import java.util.Locale

private const val UNIT = 1024.0
private val UNITS = listOf("KB", "MB", "GB", "TB", "PB")

fun formatBytes(bytes: Long): String {
    if (bytes < 0) return "\u2014"
    if (bytes < 1024) return "$bytes B"
    var value = bytes / UNIT
    var index = 0
    while (value >= UNIT && index < UNITS.lastIndex) {
        value /= UNIT
        index++
    }
    val pattern = if (value >= 100) "%.0f %s" else "%.1f %s"
    return String.format(Locale.US, pattern, value, UNITS[index])
}

fun formatBytesPerSecond(bytesPerSecond: Long): String =
    "${formatBytes(bytesPerSecond.coerceAtLeast(0))}/s"