Skip to content

Instantly share code, notes, and snippets.

@IngIeoAndSpare
Created August 22, 2019 10:49
Show Gist options
  • Select an option

  • Save IngIeoAndSpare/091f7297622e1d3abd8c1372849ee312 to your computer and use it in GitHub Desktop.

Select an option

Save IngIeoAndSpare/091f7297622e1d3abd8c1372849ee312 to your computer and use it in GitHub Desktop.
study...
function convertSize(fileSize, fixed) {
var sizeName = ['Byte','KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB'];
var sizeOffset = Math.round((fileSize.toString().length / 4));
var sizeReduceArray = [fileSize]
for(var loopCountNum = 0; loopCountNum < sizeOffset; loopCountNum++){
sizeReduceArray.push(1024);
}
const size = sizeReduceArray.reduce((acc, cur) => acc / cur).toFixed(fixed);
return `${size} ${sizeName[sizeOffset]}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment