js格式化文件大小, 输出成带单位的字符串工具
生活随笔
收集整理的这篇文章主要介绍了
js格式化文件大小, 输出成带单位的字符串工具
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
/*** 格式化文件大小, 输出成带单位的字符串* @method formatSize* @grammar formatSize( size ) => String* @grammar formatSize( size, pointLength ) => String* @grammar formatSize( size, pointLength, units ) => String* @param {Number} size 文件大小* @param {Number} [pointLength=2] 精确到的小数点数。* @param {Array} [units=[ 'B', 'K', 'M', 'G', 'TB' ]] 单位数组。从字节,到千字节,一直往上指定。如果单位数组里面只指定了到了K(千字节),同时文件大小大于M, 此方法的输出将还是显示成多少K.* @example* console.log( formatSize( 100 ) ); // => 100B* console.log( formatSize( 1024 ) ); // => 1.00K* console.log( formatSize( 1024, 0 ) ); // => 1K* console.log( formatSize( 1024 * 1024 ) ); // => 1.00M* console.log( formatSize( 1024 * 1024 * 1024 ) ); // => 1.00G* console.log( formatSize( 1024 * 1024 * 1024, 0, ['B', 'KB', 'MB'] ) ); // => 1024MB*/formatSize = function(size, pointLength, units) {var unit;units = units || [ 'B', 'K', 'M', 'G', 'TB' ];while ((unit = units.shift()) && size > 1024) {size = size / 1024;}return (unit === 'B' ? size : size.toFixed(pointLength || 2))+ unit;};
转载于:https://www.cnblogs.com/baryon/p/9611411.html
总结
以上是生活随笔为你收集整理的js格式化文件大小, 输出成带单位的字符串工具的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: mysql-二进制日志
- 下一篇: 数据分析(排序,数据特征、平均数、方差等