# Convert bigint to printable number using KB, MB, GB as needed. # Biggest string would be either 'NN.NXB' or 'NNNNXB' - 6 characters long sub ToScaledFormat { my $bignum = Math::BigFloat->new( shift ); my $logbase = shift || 1000;; my $scaling_lower_limit = 10000; if( $bignum < $scaling_lower_limit ) { return int($bignum); } my @sfx = ( '', qw( KB MB GB TB PB EB ZB YB ) ); my $log1000 = $bignum->copy->blog($logbase); my $pow1000 = int($log1000); my $wow = exp( ($log1000 - $pow1000) * log($logbase) ); # It is possible below to round up to our logbase value, which looks # wrong! If we would, instead bump up to next power set if( $wow >= ($logbase-0.5) ) { $wow /= $logbase; ++$pow1000; } # If we are not using a decimal base, we may need to show 1023KB # or such using four leading digits. Use an alternate format if( $wow >= 1000 ) { # If four significant digits return( sprintf("%.4g",$wow), $sfx[$pow1000] ); } else { return( sprintf("%.3g",$wow), $sfx[$pow1000] ); } }