JavaScript获取人类可识别的文件大小转换显示 和 格式化数字类型,保留小数点后几位,非四舍五入
function humanReadableFilesize(size) { var units = new Array("B", "KB", "MB", "GB", "TB", "PB"); var mod = 1024.0; var i = 0; while (size >= mod) { size /= mod; i++; } //return Math.round(size) + units[i]; return formatNum(size, 1) + units[i]; } //格式化数字类型,保留小数点后几位,非四舍五入 //size:值 //n:保留位数 function formatNum(size, n) { var sizeStr = size.toString(); if (sizeStr.lastIndexOf('.') > -1) return sizeStr.substring(0, sizeStr.toString().indexOf('.') + 1 + n); else return sizeStr; }