X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FUI%2Fbytecount.pm;h=7e78bf50197bb197309022d8be86fd48dd686bb0;hb=ad7f49821d40ffd099a45acc32ba91e0e211aede;hp=c2352da647440084f593a00343c349f44f53332f;hpb=b720d1c49ddb8eb87918b1344975492a87b40c33;p=freeside.git diff --git a/FS/FS/UI/bytecount.pm b/FS/FS/UI/bytecount.pm index c2352da64..7e78bf501 100644 --- a/FS/FS/UI/bytecount.pm +++ b/FS/FS/UI/bytecount.pm @@ -1,9 +1,14 @@ package FS::UI::bytecount; use strict; -use vars qw($DEBUG $me); +use vars qw($DEBUG $me @ISA @EXPORT_OK); +use Exporter; use FS::Conf; -use Number::Format; +use Number::Format 1.50; + +@ISA = qw( Exporter ); + +@EXPORT_OK = qw( bytecount_unexact parse_bytecount display_bytecount ); $DEBUG = 0; $me = '[FS::UID::bytecount]'; @@ -31,31 +36,32 @@ sub bytecount_unexact { my $bc = shift; return("$bc bytes") if ($bc < 1000); - return(sprintf("%.2f Kbytes", $bc/1000)) - if ($bc < 1000000); - return(sprintf("%.2f Mbytes", $bc/1000000)) - if ($bc < 1000000000); - return(sprintf("%.2f Gbytes", $bc/1000000000)); + return(sprintf("%.2f Kbytes", $bc/1024)) + if ($bc < 1048576); + return(sprintf("%.2f Mbytes", $bc/1048576)) + if ($bc < 1073741824); + return(sprintf("%.2f Gbytes", $bc/1073741824)); } =item parse_bytecount AMOUNT Accepts a number (digits and a decimal point) possibly followed by k, m, g, or t (and an optional 'b') in either case. Returns a pure number representing -the input or the input itself if unparsable. +the input or the input itself if unparsable. Discards commas as noise. =cut sub parse_bytecount { my $bc = shift; return $bc if (($bc =~ tr/.//) > 1); - $bc =~ /^\s*([\d.]*)\s*([kKmMgGtT]?)[bB]?\s*$/ or return $bc; + $bc =~ /^\s*([,\d.]*)\s*([kKmMgGtT]?)[bB]?\s*$/ or return $bc; my $base = $1; + $base =~ tr/,//d; return $bc unless length $base; my $exponent = index ' kmgt', lc($2); return $bc if ($exponent < 0 && $2); $exponent = 0 if ($exponent < 0); - return $base * 1024 ** $exponent; + return int($base * 1024 ** $exponent); #bytecounts are integer values } =item display_bytecount AMOUNT