Math.log()對數的妙用


 

###對數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 。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM