我使用这个函数将文件大小(以字节为单位)转换为人类可读的文件大小:

零二线函数 var i = -1; var byteUnits =[英国‘计划生育’‘兆’,‘和合’,‘PB’‘EB”、“ZB’,‘YB]; do { fileSizeInBytes /= 1024; 我+; while (fileSizeInBytes > 1024) 数学归来。max(fileSizeInBytes, 0.1)。toFixed(1) + byteUnits[i]; 的 控制台日志(getReadableFileSizeString (1551859712);//输出是“1.4 GB”

然而,这似乎不是百分之百准确的。例如:

getReadableFileSizeString(1551859712); // output is "1.4 GB"

不应该是“1.5 GB”吗?除以1024似乎失去了精度。是我完全误解了什么,还是有更好的办法?


当前回答

我写了一个大小转换函数,它也接受人类可读的格式。

JS

const bitBase = 8; const suffixes = { bit: 'b', b: 'B', kb: 'KB', mb: 'MB', gb: 'GB', tb: 'TB', }; const multipliers = { bit: { toBitHr: 1, toB: 1 / bitBase, toKB: 1 / (bitBase * 1e3), toMB: 1 / (bitBase * 1e6), toGB: 1 / (bitBase * 1e9), toTB: 1 / (bitBase * 1e12), }, B: { toBit: bitBase, toBHr: 1, toKB: 1 / 1e3, toMB: 1 / 1e6, toGB: 1 / 1e9, toTB: 1 / 1e12, }, KB: { toBit: 1 / (bitBase * 1e3), toB: 1e3, toKBHr: 1, toMB: 1 / 1e3, toGB: 1 / 1e6, toTB: 1 / 1e9, }, MB: { toBit: bitBase * 1e6, toB: 1e6, toKB: 1e3, toMBHr: 1, toGB: 1 / 1e3, toTB: 1 / 1e6, }, GB: { toBit: bitBase * 1e9, toB: 1e9, toKB: 1e6, toMB: 1e3, toGBHr: 1, toTB: 1 / 1e3, }, TB: { toBit: bitBase * 1e12, toB: 1e12, toKB: 1e9, toMB: 1e6, toGB: 1e3, toTBHr: 1, }, }; const round = (num, decimalPlaces) => { const strNum = num.toString(); const isExp = strNum.includes('e'); if (isExp) { return Number(num.toPrecision(decimalPlaces + 1)); } return Number( `${Math.round(Number(`${num}e${decimalPlaces}`))}e${decimalPlaces * -1}`, ); }; function conv( value, hr, rnd, multiplier, suffix, ) { let val = value * multiplier; if ((value * multiplier) > Number.MAX_SAFE_INTEGER) { val = Number.MAX_SAFE_INTEGER; } if (val < Number.MIN_VALUE) val = 0; if ((rnd || rnd === 0) && val < Number.MAX_SAFE_INTEGER) { val = round(val, rnd); } if (hr) return `${val}${suffix}`; return val; } const MemConv = (function _() { return { bit(value) { return { toBitHr(opts = {}) { return conv( value, true, opts.round || false, multipliers.bit.toBitHr, suffixes.bit, ); }, toB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.bit.toB, suffixes.b, ); }, toKB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.bit.toKB, suffixes.kb, ); }, toMB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.bit.toMB, suffixes.mb, ); }, toGB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.bit.toGB, suffixes.gb, ); }, toTB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.bit.toTB, suffixes.tb, ); }, }; }, B(value) { return { toBit(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.B.toBit, suffixes.bit, ); }, toBHr(opts = {}) { return conv( value, true, opts.round || false, multipliers.B.toBHr, suffixes.b, ); }, toKB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.B.toKB, suffixes.kb, ); }, toMB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.B.toMB, suffixes.mb, ); }, toGB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.B.toGB, suffixes.gb, ); }, toTB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.B.toTB, suffixes.tb, ); }, }; }, KB(value) { return { toBit(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.KB.toBit, suffixes.bit, ); }, toB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.KB.toB, suffixes.b, ); }, toKBHr(opts = {}) { return conv( value, true, opts.round || false, multipliers.KB.toKBHr, suffixes.kb, ); }, toMB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.KB.toMB, suffixes.mb, ); }, toGB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.KB.toGB, suffixes.gb, ); }, toTB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.KB.toTB, suffixes.tb, ); }, }; }, MB(value) { return { toBit(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.MB.toBit, suffixes.bit, ); }, toB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.MB.toB, suffixes.b, ); }, toKB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.MB.toKB, suffixes.kb, ); }, toMBHr(opts = {}) { return conv( value, true, opts.round || false, multipliers.MB.toMBHr, suffixes.mb, ); }, toGB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.MB.toGB, suffixes.gb, ); }, toTB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.MB.toTB, suffixes.tb, ); }, }; }, GB(value) { return { toBit(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.GB.toBit, suffixes.bit, ); }, toB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.GB.toB, suffixes.b, ); }, toKB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.GB.toKB, suffixes.kb, ); }, toMB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.GB.toMB, suffixes.mb, ); }, toGBHr(opts = {}) { return conv( value, true, opts.round || false, multipliers.GB.toGBHr, suffixes.gb, ); }, toTB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.GB.toTB, suffixes.tb, ); }, }; }, TB(value) { return { toBit(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.TB.toBit, suffixes.bit, ); }, toB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.TB.toB, suffixes.b, ); }, toKB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.TB.toKB, suffixes.kb, ); }, toMB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.TB.toMB, suffixes.mb, ); }, toGB(opts = {}) { return conv( value, opts.hr || false, opts.round || false, multipliers.TB.toGB, suffixes.gb, ); }, toTBHr(opts = {}) { return conv( value, true, opts.round || false, multipliers.TB.toTBHr, suffixes.tb, ); }, }; }, }; }()); const testCases = [1, 10, 150, 1000, 74839.67346]; const HRSuffixes = Object.values(suffixes); const roundDecimals = 2; const precision = Number(`0.${'0'.repeat(roundDecimals)}5`); const SCIENTIFIC_NOT_NUMBER_REGXP = /[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)?/g; const SUFFIX_REGXP = /[a-z]+$/i; const CONVERSION_TO_REGXP = /(?<=to).*(?=hr+$)|(?<=to).*(?=hr+$)?/i; for (const conversionFrom of (Object.keys(MemConv))) { for (const tCase of testCases) { const convFunc = MemConv[conversionFrom](tCase); for (const [conversionToFn, f] of Object.entries(convFunc)) { const conversionTo = (conversionToFn.match(CONVERSION_TO_REGXP) || [conversionToFn])[0]; const result = f(); const humanReadable = f({ hr: true }); const rounded = f({ round: roundDecimals }); const roundedAndHumanReadable = f({ hr: true, round: roundDecimals }); console.log({ value: tCase, from: conversionFrom, to: conversionTo, result, humanReadable, rounded, roundedAndHumanReadable, }); } } }

TSVersion

test

import assert from 'assert';

function test() {
  const testCases = [1, 10, 150, 1000, 74839.67346];
  const HRSuffixes = Object.values(suffixes);
  const roundDecimals = 2;
  const precision = Number(`0.${'0'.repeat(roundDecimals)}5`);
  const SCIENTIFIC_NOT_NUMBER_REGXP = /[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)?/g;
  const SUFFIX_REGXP = /[a-z]+$/i;
  const CONVERSION_TO_REGXP = /(?<=to).*(?=hr+$)|(?<=to).*(?=hr+$)?/i;

  for (const conversionFrom of (Object.keys(MemConv) as (keyof typeof MemConv)[])) {
    for (const tCase of testCases) {
      const convFunc = MemConv[conversionFrom](tCase);
      for (const [conversionToFn, f] of Object.entries(convFunc)) {
        const conversionTo = (conversionToFn.match(CONVERSION_TO_REGXP) || [conversionToFn])[0];
        const expectedSuffix = suffixes[conversionTo.toLowerCase() as keyof typeof suffixes];
        const multiplier = multipliers[conversionFrom][conversionToFn as keyof typeof multipliers[typeof conversionFrom]];
        const expectedResult = tCase * multiplier > Number.MAX_SAFE_INTEGER
            ? Number.MAX_SAFE_INTEGER
            : tCase * multiplier;

        const result = f();
        const humanReadable = f({ hr: true });
        const rounded = f({ round: roundDecimals });
        const roundedAndHumanReadable = f({ hr: true, round: roundDecimals });

        const resHrNumber = Number((humanReadable.match(SCIENTIFIC_NOT_NUMBER_REGXP) || [''])[0]);
        const resHrSuffix = (humanReadable.match(SUFFIX_REGXP) || [0])[0];
        const resRoundHrNumber = Number((roundedAndHumanReadable.match(SCIENTIFIC_NOT_NUMBER_REGXP) || [''])[0]);
        const resRoundHrSuffix = (roundedAndHumanReadable.match(SUFFIX_REGXP) || [0])[0];

        if (/hr$/i.test(conversionToFn)) {
          const resNumber = Number((humanReadable.match(SCIENTIFIC_NOT_NUMBER_REGXP) || [''])[0]);
          const resSuffix = (humanReadable.match(SUFFIX_REGXP) || [0])[0];
          assert(typeof result === 'string');
          assert(typeof resSuffix === 'string');
          assert(typeof resRoundHrNumber === 'number');
          assert(typeof rounded === 'string');
          assert(result === humanReadable);
          assert(resSuffix === expectedSuffix);
          assert(resNumber <= expectedResult + precision && resNumber >= expectedResult - precision);
        } else {
          assert(typeof result === 'number');
          assert(result === resHrNumber);
          assert(typeof rounded === 'number');
          assert(result <= expectedResult + precision && result >= expectedResult - precision);
        }

        console.log({
          value: tCase,
          from: conversionFrom,
          to: conversionToFn,
          result,
          humanReadable,
          rounded,
          roundedAndHumanReadable,
        });

        assert(typeof resHrSuffix === 'string');
        assert(typeof resHrNumber === 'number');
        assert(resHrSuffix === expectedSuffix);
        assert(resHrSuffix === resRoundHrSuffix);
        assert(HRSuffixes.includes(resHrSuffix));
      }
    }
  }
}
test();

使用

// GB to GB humanReadable
console.log(MemConv.GB(11.1942).toGBHr()); // 11.1942GB;
// GB to MB
console.log(MemConv.GB(11.1942).toMB());// 11194.2;
// MB to MB humanReadable
console.log(MemConv.MB(11.1942).toGB({ hr: true }));// 0.011194200000000001GB;
// MB to MB humanReadable with rounding
console.log(MemConv.MB(11.1942).toGB({ hr: true, round: 3 }));// 0.011GB;

其他回答

这是我写的一个:

/** * Format bytes as human-readable text. * * @param bytes Number of bytes. * @param si True to use metric (SI) units, aka powers of 1000. False to use * binary (IEC), aka powers of 1024. * @param dp Number of decimal places to display. * * @return Formatted string. */ function humanFileSize(bytes, si=false, dp=1) { const thresh = si ? 1000 : 1024; if (Math.abs(bytes) < thresh) { return bytes + ' B'; } const units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; let u = -1; const r = 10**dp; do { bytes /= thresh; ++u; } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1); return bytes.toFixed(dp) + ' ' + units[u]; } console.log(humanFileSize(1551859712)) // 1.4 GiB console.log(humanFileSize(5000, true)) // 5.0 kB console.log(humanFileSize(5000, false)) // 4.9 KiB console.log(humanFileSize(-10000000000000000000000000000)) // -8271.8 YiB console.log(humanFileSize(999949, true)) // 999.9 kB console.log(humanFileSize(999950, true)) // 1.0 MB console.log(humanFileSize(999950, true, 2)) // 999.95 kB console.log(humanFileSize(999500, true, 0)) // 1 MB

我想要“文件管理器”行为(例如,Windows资源管理器),其中小数位数与数字大小成比例。似乎没有其他答案是这样的。

function humanFileSize(size) {
    if (size < 1024) return size + ' B'
    let i = Math.floor(Math.log(size) / Math.log(1024))
    let num = (size / Math.pow(1024, i))
    let round = Math.round(num)
    num = round < 10 ? num.toFixed(2) : round < 100 ? num.toFixed(1) : round
    return `${num} ${'KMGTPEZY'[i-1]}B`
}

下面是一些例子:

humanFileSize(0)          // "0 B"
humanFileSize(1023)       // "1023 B"
humanFileSize(1024)       // "1.00 KB"
humanFileSize(10240)      // "10.0 KB"
humanFileSize(102400)     // "100 KB"
humanFileSize(1024000)    // "1000 KB"
humanFileSize(12345678)   // "11.8 MB"
humanFileSize(1234567890) // "1.15 GB"

一个简单而简短的“Pretty Bytes”函数,用于SI系统,没有不必要的分数舍入。

事实上,因为数字大小应该是人类可读的,“千分之一”的显示不再是人类的。

小数点后的位数默认为2,但可以在调用函数时修改其他值。常见的大多数显示是默认的小数点后2位。

代码很短,并使用了数字字符串三胞胎的方法。

// Simple Pretty Bytes with SI system // Without fraction rounding function numberPrettyBytesSI(Num=0, dec=2){ if (Num<1000) return Num+" Bytes"; Num =("0".repeat((Num+="").length*2%3)+Num).match(/.{3}/g); return Number(Num[0])+"."+Num[1].substring(0,dec)+" "+" kMGTPEZY"[Num.length]+"B"; } console.log(numberPrettyBytesSI(0)); console.log(numberPrettyBytesSI(500)); console.log(numberPrettyBytesSI(1000)); console.log(numberPrettyBytesSI(15000)); console.log(numberPrettyBytesSI(12345)); console.log(numberPrettyBytesSI(123456)); console.log(numberPrettyBytesSI(1234567)); console.log(numberPrettyBytesSI(12345678));

计算的另一个体现

function humanFileSize(size) {
    var i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
    return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
}

基于cocco的答案,但略有淡化(老实说,那些我觉得舒服的被保留/添加),不显示后面的0,但仍然支持0,希望对其他人有用:

函数fileSizeSI(size) { var e = (Math.log(size) / Math.log(1e3)) | 0; return +(size / Math.)战俘(1 e3, e)) .toFixed (2 ) + ' ' + (' kMGTPEZY [e - 1 ] || '') + ' B”; } / /测试: 文档。write([0, 23, 4322, 324232132, 22e9, 64.22e12, 76.22e15, 64.66e18, 77.11e21, 22e24].map(fileSizeSI).join('<br>'));