// from http://scratch99.com/web-development/javascript/convert-bytes-to-mb-kb/ | |
function bytesToSize(bytes) { | |
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | |
if (bytes == 0) return 'n/a'; | |
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); | |
if (i == 0) return bytes + ' ' + sizes[i]; | |
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i]; | |
}; |
This comment has been minimized.
This comment has been minimized.
ES6 and airbnb's eslint compliant version: function bytesToSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
if (bytes === 0) return 'n/a'
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10)
if (i === 0) return `${bytes} ${sizes[i]})`
return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`
} |
This comment has been minimized.
This comment has been minimized.
function bytesToSize(bytes) { |
This comment has been minimized.
This comment has been minimized.
With little typo fixed on line 5: function bytesToSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
if (bytes === 0) return 'n/a'
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10)
if (i === 0) return `${bytes} ${sizes[i]}`
return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`
} |
This comment has been minimized.
This comment has been minimized.
Some may doesn't like having a seperator so: function bytesToSize(bytes, seperator = "") {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
if (bytes == 0) return 'n/a'
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10)
if (i === 0) return `${bytes}${seperator}${sizes[i]}`
return `${(bytes / (1024 ** i)).toFixed(1)}${seperator}${sizes[i]}`
}
console.log( bytesToSize(2659633) ); // 2.5MB
console.log( bytesToSize(2659633, " ") ); // 2.5 MB
console.log( bytesToSize(2659633, "-") ); // 2.5-MB |
This comment has been minimized.
This comment has been minimized.
I had a need to format negative values, but function bytesToSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes === 0) return 'n/a';
const i = parseInt(Math.floor(Math.log(Math.abs(bytes)) / Math.log(1024)), 10);
if (i === 0) return `${bytes} ${sizes[i]})`;
return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`;
}
console.log(bytesToSize(1234567)); // 1.2 MB
console.log(bytesToSize(-1234567)); // -1.2 MB |
This comment has been minimized.
This comment has been minimized.
@jedfoster I used your code, but just pointing out something... If you're only rendering "Bytes", there's a dangling closing parenthesis. e.g. "Bytes)" |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
thank you |
This comment has been minimized.
This comment has been minimized.
fixed a type error on line 4, Argument of type 'number' is not assignable to parameter of type 'string'
|
This comment has been minimized.
This comment has been minimized.
A little bit of code for people who want the reverse. function printpower (n, base, power) {
if (base === 2) { // 1 << power is approx 10x faster than Math.pow(2, power)
console.log(n * (1 << power))
} else {
console.log(n * Math.pow(base, power))
}
}
const humanReadable = '86 MB'
const [n, abbreviation] = humanReadable.split(/\s+/)
if (abbreviation) {
if (/K(iB)?$/.test(abbreviation)) {
printpower(n, 2, 10)
} else if (/M(iB)?$/.test(abbreviation)) {
printpower(n, 2, 20)
} else if (/G(iB)?$/.test(abbreviation)) {
printpower(n, 2, 30)
} else if (/T(iB)?$/.test(abbreviation)) {
printpower(n, 2, 40)
} else if (/KB$/.test(abbreviation)) {
printpower(n, 10, 3)
} else if (/MB$/.test(abbreviation)) {
printpower(n, 10, 6)
} else if (/GB$/.test(abbreviation)) {
printpower(n, 10, 9)
} else if (/TB$/.test(abbreviation)) {
printpower(n, 10, 12)
}
} else {
console.log(n)
} Edit: the |
This comment has been minimized.
This comment has been minimized.
Thanks all of you |
This comment has been minimized.
This comment has been minimized.
TypeScript version: export function bytesToSize(bytes: number): string {
const sizes: string[] = ['Bytes', 'KB', 'MB', 'GB', 'TB']
if (bytes === 0) return 'n/a'
const i: number = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString())
if (i === 0) return `${bytes} ${sizes[i]}`
return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`
} |
This comment has been minimized.
Nice