Created
January 21, 2015 07:01
-
-
Save ouzhenkun/fd01172c33e8191b8a2f to your computer and use it in GitHub Desktop.
bytes 字节数转换成可读的字符
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('testApp').filter 'bytes', -> | |
(bytes, precision) -> | |
if isNaN(parseFloat(bytes)) || !isFinite(bytes) then return '-' | |
precision = 1 if !precision? | |
units = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'] | |
number = Math.floor(Math.log(bytes) / Math.log(1024)) | |
(bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment