###对数log 的妙用。。。

 

 

 formatBytes(bytes) {

        if (bytes === '0' || isNaN(bytes)) return '';

        var s = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];

        var e = Math.floor(Math.log(bytes) / Math.log(1024));

        return (bytes / Math.pow(1024, Math.floor(e))).toFixed(1) + " " + s[e];

    },

 

 

function formatBytes(nums) {
if (nums === '0' || isNaN(nums)) return '';
var s = ['s', 'm', 'H'];
var e = Math.floor(Math.log(nums) / Math.log(60));
return (nums / Math.pow(60, Math.floor(e))).toFixed(1) + ' ' + s[e];
}

 

 

总结: 存在规律的进制转换的情况,比如  mb和byte都是1024 ;比如s秒和m分 都是60 。

 

相关文章:

  • 2022-01-27
  • 2022-12-23
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
  • 2021-06-15
  • 2021-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-22
  • 2021-10-10
  • 2021-06-11
  • 2022-12-23
  • 2022-01-15
  • 2022-12-23
相关资源
相似解决方案