Flash计算文件大小裁剪小数点与获取单位

作者: 2010-08-16,12:16 上午 周一

以前PHP写过一个类似的函数,现在换到AS3写,可以输入一个字节数的大小,可以输出类似于,5.03 KB,30.5 TB等这样的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 /**
  * 给一个文件的大小,换算出大小
  * @param  bytes           文件的大小,字节数
  * @param  decimals = 1    小数点后的
  * @return
  */

public static function getFileSize(bytes:Number, decimals = 1):String {
    var FileSizeName:String = "Bytes";
    var endBytes:Number;
    if (bytes >= 1152921504606846976) {
        FileSizeName = "EB";
        endBytes = bytes / 1152921504606846976;
    }else if (bytes >= 1125899906842624) {
        FileSizeName = "PB";
        endBytes = bytes / 1125899906842624;
    }else if (bytes >= 1099511627776) {
        FileSizeName = "TB";
        endBytes = bytes / 1099511627776;
    }else if (bytes >= 1073741824) {
        FileSizeName = "GB";
        endBytes = bytes / 1073741824;
    }else if (bytes >= 1048576) {
        FileSizeName = "MB";
        endBytes = bytes / 1048576;
    }else if (bytes >= 1024) {
        FileSizeName = "KB";
        endBytes = bytes / 1024;
    }else {
        return bytes + " " + FileSizeName;
    }
    var decimalsNum:Number = 1;
    for (var i:int = 0; i < decimals; i++) {
        decimalsNum = decimalsNum * 10;
    }
    endBytes = Math.round(endBytes * decimalsNum) / decimalsNum;
    return endBytes + " " + FileSizeName;
}

暂无相关文章

分类 : FLASH (阅览:) Tags :

评论

还没有评论呢。


留下评论


|:victory:| |:tongue:| |:titter:| |:time:| |:sweat:| |:smile:| |:shy:| |:shocked:| |:sad:| |:mad:| |:lol:| |:kiss:| |:hug:| |:huffy:| |:handshake:| |:cry:| |:call:| |:biggrin:|