showing results for - "kb to mb js"
Emma
24 Feb 2017
1const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
2
3function niceBytes(x){
4
5  let l = 0, n = parseInt(x, 10) || 0;
6
7  while(n >= 1024 && ++l){
8      n = n/1024;
9  }
10  //include a decimal point and a tenths-place digit if presenting 
11  //less than ten of KB or greater units
12  return(n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]);
13}