我通过PHP获得了this code个字节来转换大小.

现在我想使用JavaScript将这些大小转换为human readable个大小.我try 将此代码转换为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;
}

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

推荐答案

由此:(source)

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];
}

Note:这是原始代码,请使用下面的固定版本.


Fixed version, unminified and ES6'ed:(按社区)

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];
}

Fixed Version (by StackOverflow's community, minified by 100)

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]}}

Usage :

// formatBytes(bytes,decimals)

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

Demo / source :

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 : Change 100 or 101 as you want (102 or 103)

Javascript相关问答推荐

如何避免使用ajax在Vue 3合成API中重定向

如何循环访问对象数组并以关键值形式获得结果?

JS、C++和C#给出不同的Base 64 Guid编码结果

JS生成具有给定数字和幻灯片计数的数组子集

是什么原因导致此Angular 16电影应用程序中因类型错误而不存在属性?

if/else JavaScript中的条件行为

自定义高图中的x轴标签序列

你怎么看啦啦队的回应?

在react JS中映射数组对象的嵌套数据

我的角模板订阅后不刷新'

变量的值在Reaction组件中的Try-Catch语句之外丢失

您能在卸载程序(QtInsteller框架)上添加WizardPage吗?

React.Development.js和未捕获的ReferenceError:未定义useState

面对代码中的错误作为前端与后端的集成

如何利用CSS中的隐藏元素实现平滑扩展和防止网格行间隙

如何使用画布在另一个内部绘制一个较小但相同的形状,同时保持恒定的边界厚度?

用于测试其方法和构造函数的导出/导入类

P5.js中矩形内的圆弧

在没有任何悬停或其他触发的情况下连续交换图像

使用Perl Selify::Remote::Driver执行Java脚本时出错