Fix size string rounding issues
This commit is contained in:
parent
6b4de8d6df
commit
2111222d2f
1 changed files with 2 additions and 2 deletions
|
@ -4,10 +4,10 @@ package eu.depau.ddroid.utils
|
||||||
|
|
||||||
private fun <T> humanReadableByteCount(bytes: T, si: Boolean = false): String where T : Comparable<T>, T : Number {
|
private fun <T> humanReadableByteCount(bytes: T, si: Boolean = false): String where T : Comparable<T>, T : Number {
|
||||||
val unit: Long = if (si) 1000 else 1024
|
val unit: Long = if (si) 1000 else 1024
|
||||||
if (bytes.toLong() < unit) return bytes.toString() + " B"
|
if (bytes.toLong() < unit) return String.format("%.2f B", bytes)
|
||||||
val exp = (Math.log(bytes.toDouble()) / Math.log(unit.toDouble())).toInt()
|
val exp = (Math.log(bytes.toDouble()) / Math.log(unit.toDouble())).toInt()
|
||||||
val pre = (if (si) "kMGTPE" else "KMGTPE")[exp - 1] + if (si) "" else "i"
|
val pre = (if (si) "kMGTPE" else "KMGTPE")[exp - 1] + if (si) "" else "i"
|
||||||
return String.format("%.1f %sB", bytes.toDouble() / Math.pow(unit.toDouble(), exp.toDouble()), pre)
|
return String.format("%.2f %sB", bytes.toDouble() / Math.pow(unit.toDouble(), exp.toDouble()), pre)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Long.toHRSize(si: Boolean = false) = humanReadableByteCount(this, si)
|
fun Long.toHRSize(si: Boolean = false) = humanReadableByteCount(this, si)
|
||||||
|
|
Loading…
Reference in a new issue