[Templates] print "n" nicely if n = bytes
Randal L. Schwartz
merlyn@stonehenge.com
16 Sep 2006 16:22:26 -0700
>From an upcoming magazine article (http://www.stonehenge.com/merlyn/UnixReview/col67.html):
[% MACRO nice(n) BLOCK;
IF n >= 1073741824 * 9.995;
n / 1073741824 | format('%dG');
ELSIF n >= 1073741824;
n / 1073741824 | format('%.2gG');
ELSIF n >= 1048576 * 9.995;
n / 1048576 | format('%dM');
ELSIF n >= 1048576;
n / 1048576 | format('%.2gM');
ELSIF n >= 1024 * 9.995;
n / 1024 | format('%dK');
ELSIF n >= 1024;
n / 1024 | format('%.2gK');
ELSE;
n;
END;
END %]
And from the text (which you can't yet see):
Lines 51 to 67 define a macro to take a number and format it nicely into 4
characters or less (up to a terabyte, and after that, you're on your own,
you lucky dog). I tried using Template::Plugin::Number::Format and it
didn't do what I wanted, so I wrote my own. I use this both for the bytes
and for the file count, simply by applying the BLOCKSIZE scaling factor as
needed.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!