Skip to content

文件大小转化, 数组表示

js
/**
 * @param {number} size The size of file.
 * @return {number[]} The array of fileSize, and from index [0] to [4] means B, KB, MB, GB and TB.
 */
const filterSizeList = (size) => {
  let temp = size;
  const units = [0, 0, 0, 0, 0]; // B, KB, MB, GB, TB

  for (let i = 4; i >= 1; i--) {
    if (temp >= Math.pow(1024, i)) {
      units[i] = Math.floor(temp / Math.pow(1024, i));
      temp = temp % Math.pow(1024, i);
    }
  }

  units[0] = temp
  return units
};
2025( )
今日 8.33%
本周 42.86%
本月 48.39%
本年 4.11%
Powered by Snowinlu | Copyright © 2024- | MIT License