1 package FS::UI::bytecount;
4 use vars qw($DEBUG $me);
9 $me = '[FS::UID::bytecount]';
13 FS::UI::bytecount - Subroutines for parsing and displaying byte counters
17 use FS::UI::bytecount;
23 =item bytecount_unexact COUNT
25 Returns a two decimal place value for COUNT followed by bytes, Kbytes, Mbytes,
26 or GBytes as appropriate.
30 sub bytecount_unexact {
34 return(sprintf("%.2f Kbytes", $bc/1000))
36 return(sprintf("%.2f Mbytes", $bc/1000000))
37 if ($bc < 1000000000);
38 return(sprintf("%.2f Gbytes", $bc/1000000000));
41 =item parse_bytecount AMOUNT
43 Accepts a number (digits and a decimal point) possibly followed by k, m, g, or
44 t (and an optional 'b') in either case. Returns a pure number representing
45 the input or the input itself if unparsable.
51 return $bc if (($bc =~ tr/.//) > 1);
52 $bc =~ /^\s*([\d.]*)\s*([kKmMgGtT]?)[bB]?\s*$/ or return $bc;
54 return $bc unless length $base;
55 my $exponent = index ' kmgt', lc($2);
56 return $bc if ($exponent < 0 && $2);
57 $exponent = 0 if ($exponent < 0);
58 return $base * 1024 ** $exponent;
61 =item display_bytecount AMOUNT
63 Converts a pure number to a value followed possibly followed by k, m, g, or
68 sub display_bytecount {
70 return $bc unless ($bc =~ /^(\d+)$/);
71 my $conf = new FS::Conf;
72 my $f = new Number::Format;
73 my $precision = ( $conf->exists('datavolume-significantdigits') &&
74 $conf->config('datavolume-significantdigits') =~ /^\s*\d+\s*$/ )
75 ? $conf->config('datavolume-significantdigits')
77 my $unit = $conf->exists('datavolume-forcemegabytes') ? 'M' : 'A';
79 return $f->format_bytes($bc, precision => $precision, unit => $unit);