s3-share/web/sizeToReadable.ts

11 lines
318 B
TypeScript
Raw Permalink Normal View History

2022-05-13 13:33:29 +00:00
function sizeToReadable(size: number): string {
if (size == 0)
return "0 B"
const i = Math.floor(Math.log(size) / Math.log(1024))
const num = (size / Math.pow(1024, i))
return (num.toFixed(1).endsWith("0")?num.toFixed(0):num.toFixed(1)) + " " + ["B", "kB", "MB", "GB", "TB"][i]
}
export default sizeToReadable