X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;ds=sidebyside;f=ND%2FInclude.pm;fp=ND%2FInclude.pm;h=421d81ea044e47f3149b0a4d1c24787235cc0113;hb=33379bf7e2909a0f46ccf771a919121e48f9ab41;hp=53e7a4c17580a48c1e26c2c0d6d36b0690547d51;hpb=b8985d0163efd6e9af29dcca7a7689183286c7e7;p=ndwebbie.git diff --git a/ND/Include.pm b/ND/Include.pm index 53e7a4c..421d81e 100644 --- a/ND/Include.pm +++ b/ND/Include.pm @@ -25,7 +25,7 @@ require Exporter; our @ISA = qw/Exporter/; -our @EXPORT = qw/min max log_message intel_log unread_query/; +our @EXPORT = qw/min max parseValue prettyValue log_message intel_log unread_query/; sub min { my ($x,$y) = @_; @@ -37,6 +37,30 @@ sub max { return ($x < $y ? $y : $x); } + +sub parseValue { + if (defined $_[0] && $_[0] =~ /^(-?\d+(?:\.\d+)?)([khMG])?$/){ + return $1 unless defined $2; + return $1*100 if $2 eq 'h'; + return $1*1000 if $2 eq 'k'; + return $1*1000000 if $2 eq 'M'; + return $1*1000000000 if $2 eq 'G'; + } + return $_[0]; +} + +sub prettyValue { + my ($value) = @_; + my $unit = ''; + my @units = ('k','M','G','T'); + for (my $i = 0; $value >= 1000;$i++){ + $value /= 1000; + $unit = $units[$i]; + } + return sprintf('%.2f%s', $value,$unit); +} + + sub log_message { my ($uid, $message) = @_; my $log = $ND::DBH->prepare_cached(q{INSERT INTO forum_posts (ftid,uid,message) VALUES(