JavaScript字节转换为KB、MB、GB的函数

<FONT face=宋体>&lt;script&gt;<BR>function convertSize(size) {<BR>&nbsp;&nbsp;&nbsp; if(!size) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return '0 Bytes';<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; var sizeNames = [' Bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'];<BR>&nbsp;&nbsp;&nbsp; var i = Math.floor(Math.log(size)/Math.log(1024));<BR>&nbsp;&nbsp;&nbsp; var p = (i &gt; 1) ? 2 : 0;<BR>&nbsp;&nbsp;&nbsp; return (size/Math.pow(1024, Math.floor(i))).toFixed(p) + sizeNames[i];<BR>}<BR>alert(convertSize(1234));&nbsp; <BR>&lt;/script&gt;</FONT>
by 雪洁 2011-12-15 16:08:05 6465 views

1 Comments

游客 2012-08-03 11:26:58
寒舍 www.hanshe.org
我来说几句

相关文章