Skip to content

Instantly share code, notes, and snippets.

@ouzhenkun
Created January 21, 2015 07:01
Show Gist options
  • Save ouzhenkun/fd01172c33e8191b8a2f to your computer and use it in GitHub Desktop.
Save ouzhenkun/fd01172c33e8191b8a2f to your computer and use it in GitHub Desktop.
bytes 字节数转换成可读的字符
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