我得到这段代码通过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;
}

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


当前回答

function formatBytes(bytes) {
    var marker = 1024; // Change to 1000 if required
    var decimal = 3; // Change as required
    var kiloBytes = marker; // One Kilobyte is 1024 bytes
    var megaBytes = marker * marker; // One MB is 1024 KB
    var gigaBytes = marker * marker * marker; // One GB is 1024 MB
    var teraBytes = marker * marker * marker * marker; // One TB is 1024 GB

    // return bytes if less than a KB
    if(bytes < kiloBytes) return bytes + " Bytes";
    // return KB if less than a MB
    else if(bytes < megaBytes) return(bytes / kiloBytes).toFixed(decimal) + " KB";
    // return MB if less than a GB
    else if(bytes < gigaBytes) return(bytes / megaBytes).toFixed(decimal) + " MB";
    // return GB if less than a TB
    else return(bytes / gigaBytes).toFixed(decimal) + " GB";
}

其他回答

const byteConversion = (bytes: number, decimals = 2) => {
if (bytes === 0) return '0 B';

const kiloByte = 1000;
const decimal = decimals < 0 ? 0 : decimals;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

const i: number = Math.floor(Math.log(bytes) / Math.log(kiloByte));

return `${parseFloat((bytes / kiloByte ** i).toFixed(decimal))} ${sizes[i]}`;

};

函数by睾丸(字节){ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; If (bytes === 0)返回'n/a'; const i = Math.min(Math.floor(Math.log(bytes) / Math.log(1024))),大小。长度- 1); 如果(i === 0)返回' ${bytes} ${sizes[i]} '; 返回“${(字节/(1024 * *我)).toFixed(1)} ${大小[我]}'; } console.log (bytesToSize (400000)) console.log (bytesToSize (5000000))

将字节格式化为最符合逻辑大小(KB、MB或GB)的实用程序方法。

Number.prototype.formatBytes = function() { var单位= ['B', 'KB', 'MB', 'GB', 'TB'], 字节= this, 我; For (i = 0;>= 1024 && I < 4;我+ +){ 字节/= 1024; } return bytes.toFixed(2) + units[i]; } 令a = 235678;/ /字节 console.log (a.formatBytes ());//结果为230.15KB

使用位操作将是一个更好的解决方案。试试这个

function formatSizeUnits(bytes)
{
    if ( ( bytes >> 30 ) & 0x3FF )
        bytes = ( bytes >>> 30 ) + '.' + ( bytes & (3*0x3FF )) + 'GB' ;
    else if ( ( bytes >> 20 ) & 0x3FF )
        bytes = ( bytes >>> 20 ) + '.' + ( bytes & (2*0x3FF ) ) + 'MB' ;
    else if ( ( bytes >> 10 ) & 0x3FF )
        bytes = ( bytes >>> 10 ) + '.' + ( bytes & (0x3FF ) ) + 'KB' ;
    else if ( ( bytes >> 1 ) & 0x3FF )
        bytes = ( bytes >>> 1 ) + 'Bytes' ;
    else
        bytes = bytes + 'Byte' ;
    return bytes ;
}

function bytesToSize(bytes) { var sizes = ['B', 'K', 'M', 'G', 'T', 'P']; for (var i = 0; i < sizes.length; i++) { if (bytes <= 1024) { return bytes + ' ' + sizes[i]; } else { bytes = parseFloat(bytes / 1024).toFixed(2) } } return bytes + ' P'; } console.log(bytesToSize(234)); console.log(bytesToSize(2043)); console.log(bytesToSize(20433242)); console.log(bytesToSize(2043324243)); console.log(bytesToSize(2043324268233)); console.log(bytesToSize(2043324268233343));