Question : how to convert byte size into human readable format in java
Answered by : hannan
fun bytesIntoHumanReadable(bytes: Long): String { val kilobyte: Long = 1024 val megabyte = kilobyte * 1024 val gigabyte = megabyte * 1024 val terabyte = gigabyte * 1024 return if (bytes >= 0 && bytes < kilobyte) { "$bytes B" } else if (bytes >= kilobyte && bytes < megabyte) { (bytes / kilobyte).toString() + " KB" } else if (bytes >= megabyte && bytes < gigabyte) { (bytes / megabyte).toString() + " MB" } else if (bytes >= gigabyte && bytes < terabyte) { (bytes / gigabyte).toString() + " GB" } else if (bytes >= terabyte) { (bytes / terabyte).toString() + " TB" } else { "$bytes Bytes" } }
Source : | Last Update : Fri, 15 Apr 22