Skip to content

文件大小转化

js
/**
 * @param {number} size The size of file.
 * @return {string} The string after transfer e.g. 512MB.
 */
const filterSize = (size) => {
  if (!size) return "-";
  if (size < Math.pow(1024, 1)) return size + " B";
  if (size < Math.pow(1024, 2))
    return (size / Math.pow(1024, 1)).toFixed(2) + " KB";
  if (size < Math.pow(1024, 3))
    return (size / Math.pow(1024, 2)).toFixed(2) + " MB";
  if (size < Math.pow(1024, 4))
    return (size / Math.pow(1024, 3)).toFixed(2) + " GB";
  return (size / Math.pow(1024, 4)).toFixed(2) + " TB";
};
2025( )
今日 8.33%
本周 42.86%
本月 48.39%
本年 4.11%
Powered by Snowinlu | Copyright © 2024- | MIT License