在 JavaScript 中将字节大小转换为 KB、MB、GB 的正确方法

新手上路,请多包涵

我得到 这个代码 来通过 PHP 以字节为单位转换大小。

现在我想使用 JavaScript 将这些尺寸转换为 人类可读 的尺寸。我尝试将此代码转换为 JavaScript,如下所示:

function formatSizeUnits(bytes){
  if      (bytes >= 1073741824) { bytes = (bytes / 1073741824).toFixed(2) + " GB"; }
  else if (bytes >= 1048576)    { bytes = (bytes / 1048576).toFixed(2) + " MB"; }
  else if (bytes >= 1024)       { bytes = (bytes / 1024).toFixed(2) + " KB"; }
  else if (bytes > 1)           { bytes = bytes + " bytes"; }
  else if (bytes == 1)          { bytes = bytes + " byte"; }
  else                          { bytes = "0 bytes"; }
  return bytes;
}

这是这样做的正确方法吗?有没有更简单的方法?

原文由 l2aelba 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 4.5k
2 个回答

由此:( 来源

 function bytesToSize(bytes) {
 var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
 if (bytes == 0) return '0 Byte';
 var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
 return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
 }

注意: 这是原始代码,请使用下面的固定版本。


固定版本,未缩小和 ES6 版本:( 按社区)

 function formatBytes(bytes, decimals = 2) {
 if (bytes === 0) return '0 Bytes';

 const k = 1024;
 const dm = decimals < 0 ? 0 : decimals;
 const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

 const i = Math.floor(Math.log(bytes) / Math.log(k));

 return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
 }

固定版本 (由 StackOverflow 的社区提供,由 JSCompress 缩小)

 function formatBytes(a,b=2,k=1024){with(Math){let d=floor(log(a)/log(k));return 0==a?"0 Bytes":parseFloat((a/pow(k,d)).toFixed(max(0,b)))+" "+["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"][d]}}

用法 :

 // formatBytes(bytes,decimals)

 formatBytes(1024); // 1 KB
 formatBytes('1024'); // 1 KB
 formatBytes(1234); // 1.21 KB
 formatBytes(1234, 3); // 1.205 KB

演示/来源:

 function formatBytes(bytes, decimals = 2) {
 if (bytes === 0) return '0 Bytes';

 const k = 1024;
 const dm = decimals < 0 ? 0 : decimals;
 const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

 const i = Math.floor(Math.log(bytes) / Math.log(k));

 return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
 }

 // ** Demo code **
 var p = document.querySelector('p'),
 input = document.querySelector('input');

 function setText(v){
 p.innerHTML = formatBytes(v);
 }
 // bind 'input' event
 input.addEventListener('input', function(){
 setText( this.value )
 })
 // set initial text
 setText(input.value);
 <input type="text" value="1000">
 <p></p>

PS:根据需要更改 k = 1000sizes = ["..."]字节

原文由 Jack 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果呈现小于 10 KB 或更大的单位,则此解决方案包括小数点和十分位数字

const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

function niceBytes(x){

  let l = 0, n = parseInt(x, 10) || 0;

  while(n >= 1024 && ++l){
      n = n/1024;
  }

  return(n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]);
}

结果:

 niceBytes(435)                 // 435 bytes
niceBytes(3398)                // 3.3 KB
niceBytes(490398)              // 479 KB
niceBytes(6544528)             // 6.2 MB
niceBytes(23483023)            // 22 MB
niceBytes(3984578493)          // 3.7 GB
niceBytes(30498505889)         // 28 GB
niceBytes(9485039485039445)    // 8.4 PB

原文由 Faust 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题