Ancient thread necropsy, but here's a better macro (IMO). It's ugly but it works just like the -h option on many GNU tools.
Usage:
| eval readable_size=`readable(size)`
Definition: (as seen in Settings -> Adv Search -> Search macros -> new:
if( $num$ < 1024, tostring($num$), if ( (floor($num$/pow(1024,floor(log($num$,1024))))) < 10
, ( (tostring((floor($num$/pow(1024,floor(log($num$,1024)))))) + ".") + tostring(round((($num$/pow(1024,floor(log($num$,1024))))-(floor($num$/pow(1024,floor(log($num$,1024))))))*10))) + (substr("KMGTPEZY",floor(log($num$,1024)),1))
, ( tostring((floor($num$/pow(1024,floor(log($num$,1024)))))) + (substr("KMGTPEZY",floor(log($num$,1024)),1)) )
) )
Not an eval-based definition (unchecked)
Arguments: num
Validation Expression: !isnum($num$)
Validation Error Message: Numeric value required
My key observation for the algorithm is that the log base 1024 will give you the "scale"-- KB or PB or whatever, by dropping the fractional part (i.e. log_10(5.6MB) = 2 -> M).
In working on this, I used meaningful names and replace-all'd them to fundamental eval functions. Here's the pseudocode:
if $num$ < 1024:
printf("%4d", $num$)
else
if $num$ reduces to a single digit
# print in the form x.yS
printf( "%d.%d%c", whole_part(reduction), 1st digit of frac_part(reduction), KMGTPEZY suffix appropriate for this scale
else # This is actually the most common case. The result is just the whole part of the reduction and the suffix
printf("%3d%s", whole_part(reduction), suffix)
Hope this helps somebody
--Joe
... View more