Subject: | optionally force format_bytes what units to use |
It would be nice to permit format_bytes to, for instance, return a number in terms of M, even if the number exceeds 1G or is less than 1M. Right now, that's not possible.
I've enclosed a patch, which includes documentation changes and a fix
for the 1K bug I just submitted, although some of the things I've done here are probably not the best ways to do it.
--- Format.pm.orig Tue Apr 27 13:57:38 2004
+++ Format.pm Tue Apr 27 14:43:40 2004
@@ -644,13 +644,13 @@
##----------------------------------------------------------------------
-=item format_bytes($number, $precision)
+=item format_bytes($number, $precision, $unit)
Returns a string containing C<$number> formatted similarly to
-C<format_number()>, except that if the number is over 1024, it will be
-divided by 1024 and "K" appended to the end; or if it is over 1048576
-(1024*1024), it will be divided by 1048576 and "M" appended to the
-end. Negative values will result in an error.
+C<format_number()>, except that if the number is equal to or over
+1024, it will be divided by 1024 and "K" appended to the end; or if it
+is equal to or over 1048576 (1024*1024), it will be divided by 1048576
+and "M" appended to the end. Negative values will result in an error.
If C<$precision> is not provided, the default of 2 will be used.
Examples:
@@ -659,27 +659,58 @@
format_bytes(2048) yields '2K'
format_bytes(1048576) yields '1M'
+C<$unit> is optional, and may be 'G', 'M', 'K', or 'B'; if present,
+the default units used for the results (which, as described above, are
+based on the size of the number) will be overriden by the unit
+requested. E.g.:
+
+ format_bytes(1048576, undef, 'K') yields '1,024K'
+ instead of '1M'
+ format_bytes(1048577, 3, 'K') yields '1,024.001K'
+ instead of '1M'
+
+Using 'B' as the unit blocks all unit conversion, and the function
+simply returns the result of format_number($number, $precision).
+
+Note that 'G', 'M', and 'K' are fixed, and do not vary even when the
+$GIGA_SUFFIX, $MEGA_SUFFIX, and $KILO_SUFFIX have been changed.
+
=cut
sub format_bytes
{
- my ($self, $number, $precision) = _get_self @_;
+ my ($self, $number, $precision, $unit) = _get_self @_;
$precision = $self->{decimal_digits} unless defined $precision;
$precision = 2 unless defined $precision; # default
croak "Negative number ($number) not allowed in format_bytes\n"
if $number < 0;
my $suffix = "";
- if ($number > 0x40000000)
+ if ($unit) {
+ # More liberal in what is accepted than the POD says;
+ # either rewrite the POD or this?
+ # Also: while just using 'K' is more compact, probably
+ # best to still allow whatever is the
+ # $KILO_SUFFIX as a valid value.
+ if ($unit =~ /^([GMKBgmkb])[Bb]?$/) { $unit = uc $1; }
+ else { croak "Unknown unit ($unit)\n"; }
+ }
+ else {
+ if ($number >= 0x40000000) { $unit = 'G'; }
+ elsif ($number >= 0x100000) { $unit = 'M'; }
+ elsif ($number >= 0x400) { $unit = 'K'; }
+ }
+
+ if ($unit eq 'G')
{
$number /= 0x40000000;
$suffix = $self->{giga_suffix};
}
- elsif ($number > 0x100000)
+ elsif ($unit eq 'M')
{
$number /= 0x100000;
$suffix = $self->{mega_suffix};
}
- elsif ($number > 0x400)
+ elsif ($unit eq 'K')
{
$number /= 0x400;
$suffix = $self->{kilo_suffix};