js中字节B转化成KB,MB,GB
程序员文章站
2022-04-14 16:12:18
function change(limit){ var size = ""; if(limit < 0.1 * 1024){ //小于0.1KB,则转化成B size = limit.toFixed(2) + "B" }else if(limit < 0.1 * 1024 * 1024){ //小于 ......
function change(limit){
var size = "";
if(limit < 0.1 * 1024){ //小于0.1kb,则转化成b
size = limit.tofixed(2) + "b"
}else if(limit < 0.1 * 1024 * 1024){ //小于0.1mb,则转化成kb
size = (limit/1024).tofixed(2) + "kb"
}else if(limit < 0.1 * 1024 * 1024 * 1024){ //小于0.1gb,则转化成mb
size = (limit/(1024 * 1024)).tofixed(2) + "mb"
}else{ //其他转化成gb
size = (limit/(1024 * 1024 * 1024)).tofixed(2) + "gb"
}
var sizestr = size + ""; //转成字符串
var index = sizestr.indexof("."); //获取小数点处的索引
var dou = sizestr.substr(index + 1 ,2) //获取小数点后两位的值
if(dou == "00"){ //判断后两位是否为00,如果是则删除00
return sizestr.substring(0, index) + sizestr.substr(index + 3, 2)
}
return size;
}
var newlimit = change(1048576);
console.log(newlimit);
上一篇: mac安装的软件和idea的插件推荐