1 package FS::UI::bytecount;
4 use vars qw($DEBUG $me);
6 use Number::Format 1.50;
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. Discards commas as noise.
51 return $bc if (($bc =~ tr/.//) > 1);
52 $bc =~ /^\s*([,\d.]*)\s*([kKmMgGtT]?)[bB]?\s*$/ or return $bc;
55 return $bc unless length $base;
56 my $exponent = index ' kmgt', lc($2);
57 return $bc if ($exponent < 0 && $2);
58 $exponent = 0 if ($exponent < 0);
59 return int($base * 1024 ** $exponent); #bytecounts are integer values
62 =item display_bytecount AMOUNT
64 Converts a pure number to a value followed possibly followed by k, m, g, or
69 sub display_bytecount {
71 return $bc unless ($bc =~ /^(\d+)$/);
72 my $conf = new FS::Conf;
73 my $f = new Number::Format;
74 my $precision = ( $conf->exists('datavolume-significantdigits') &&
75 $conf->config('datavolume-significantdigits') =~ /^\s*\d+\s*$/ )
76 ? $conf->config('datavolume-significantdigits')
78 my $unit = $conf->exists('datavolume-forcemegabytes') ? 'M' : 'A';
80 return $f->format_bytes($bc, precision => $precision, unit => $unit);