Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript中将字节大小转换为KB,MB,GB的正确方法(Correct way to convert size in bytes to KB, MB, GB in JavaScript)

I got this code to covert size in bytes via PHP.

(我通过PHP 将此代码转换为秘密大小(以字节为单位)。)

Now I want to convert those sizes to human readable sizes using JavaScript.

(现在,我想使用JavaScript将这些尺寸转换为人类可读的尺寸。)

I tried to convert this code to JavaScript, which looks like this:

(我试图将此代码转换为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;
}

Is this the correct way of doing this?

(这是正确的方法吗?)

Is there an easier way?

(有没有更简单的方法?)

  ask by l2aelba translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From this: ( 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 : This is original code, Please use fixed version below.

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

Aliceljm does not active her copied code anymore

(Aliceljm不再激活她复制的代码)


Now, Fixed version unminified, and ES6'ed: (by community)

(现在,未缩小固定版本,并且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];
}

Now, Fixed version : (by Stackoverflow's community, + Minified by JSCompress )

(现在,固定版本:( 由Stackoverflow的社区提供,+由JSCompress缩小)

function formatBytes(a,b){if(0==a)return"0 Bytes";var c=1024,d=b||2,e=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],f=Math.floor(Math.log(a)/Math.log(c));return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f]}

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) { if(bytes == 0) return '0 Bytes'; var k = 1024, dm = decimals <= 0 ? 0 : decimals || 2, sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 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 k = 1000 or sizes = ["..."] as you want ( bits or bytes )

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...