存储字节kb,mb,gb,tb转换代码

$(function(){
    $(".c_block .txt").keyup(function(){
        var current_unit=$(this).attr('name'),num=parseFloat($(this).val()),key_obj=this;
        if(isNaN(num)){return;}
        var d=parseInt($('#decimals').val());if(isNaN(d)){d=6;}
        $(".c_block .txt").each(function(){
            if(key_obj==this){return;}
            $(this).val(byte_calc.Round(byte_calc.conver(num,current_unit,$(this).attr('name')),d));
        });
    });
});

var byte_calc={
    serial:'tb_gb_mb_kb_byte_bit',
    tb_one:1024,
    gb_one:1024,
    mb_one:1024,
    kb_one:1024,
    byte_one:8,
    byte_bit:function(num){ 
        return num * byte_calc.byte_one;
    },
    kb_byte:function(num){
        return num * byte_calc.kb_one;
    },
    mb_kb:function(num){
        return num * byte_calc.mb_one;
    },
    gb_mb:function(num){
        return num * byte_calc.gb_one;
    },
    tb_gb:function(num){
        return num * byte_calc.tb_one;
    },
    big_small:function(num,b,s){
        var linkstr=byte_calc.serial.split('_'),name='';
        for(i in linkstr){
            if(name!=''&&linkstr[i]==s){
                eval('num=byte_calc.'+name+'_'+linkstr[i]+'('+num+')');
                return num;
            }else if(name!=''){
                eval('num=byte_calc.'+name+'_'+linkstr[i]+'('+num+')');
                name=linkstr[i];
            }
            if(linkstr[i]==b){name=linkstr[i];}
        }
        return false;
    },
    small_big:function(num,s,b){
        var n=byte_calc.big_small(1,b,s);
        return num/n;
    },
    conver:function(num,s,d){
        var bc_s=byte_calc.serial;
        if(bc_s.indexOf(s)<0||bc_s.indexOf(d)<0){alert('不能转换的存储单位')}
        if(s==d){return num;}
        if(bc_s.indexOf(s)<bc_s.indexOf(d)){
            return byte_calc.big_small(num,s,d);
        }else{
            return byte_calc.small_big(num,s,d)
        }
    },
    Round:function(i,digit)
    {
        if(digit==0)
            p=1;
        else
        {
            if(digit)
                p=Math.pow(10,digit);
            else
                p=100;
        }
        return Math.round(i*p)/p;
    }
}

  演示地址:http://www.bangnishouji.com/tools/Byte_calculate.html

posted on 2013-12-02 16:21  食神网技术  阅读(297)  评论(0编辑  收藏  举报