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-二进制日志
- 下一篇: 数据分析(排序,数据特征、平均数、方差等