/usr/share/perl5/SHARYANTO/Number/Util.pm is in libsharyanto-utils-perl 0.53-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | package SHARYANTO::Number::Util;
use 5.010001;
use locale;
use strict;
use utf8;
use warnings;
our $VERSION = '0.53'; # VERSION
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(format_metric);
sub format_metric {
my ($num, $opts) = @_;
$opts //= {};
$opts->{base} //= 2;
my $im = $opts->{i_mark} // 1;
my $base0 = $opts->{base};
my $base = $base0 == 2 ? 1024 : 1000;
my $rank;
my $prefix;
if ($num == 0) {
$rank = 0;
$prefix = "";
} else {
$rank = int(log(abs($num))/log($base));
if ($rank == 0 && abs($num) >= 1) { $prefix = "" }
elsif ($rank == 1) { $prefix = $im && $base0==10 ? "ki" : "k" } # kilo
elsif ($rank == 2) { $prefix = $im && $base0==10 ? "Mi" : "M" } # mega
elsif ($rank == 3) { $prefix = $im && $base0==10 ? "Gi" : "G" } # giga
elsif ($rank == 4) { $prefix = $im && $base0==10 ? "Ti" : "T" } # tera
elsif ($rank == 5) { $prefix = $im && $base0==10 ? "Pi" : "P" } # peta
elsif ($rank >= 8) { $prefix = $im && $base0==10 ? "Yi" : "Y" } # yotta
elsif ($rank == 7) { $prefix = $im && $base0==10 ? "Zi" : "Z" } # zetta
elsif ($rank == 6) { $prefix = $im && $base0==10 ? "Ei" : "E" } # exa
elsif ($rank == 0) { $prefix = "m" } # milli
elsif ($rank == -1) { $prefix = "μ" } # micro
elsif ($rank == -2) { $prefix = "n" } # nano
elsif ($rank == -3) { $prefix = "p" } # pico
elsif ($rank == -4) { $prefix = "f" } # femto
elsif ($rank == -5) { $prefix = "a" } # atto
elsif ($rank == -6) { $prefix = "z" } # zepto
elsif ($rank <= -7) { $prefix = "y" } # yocto
}
my $prec = $opts->{precision} // 1;
$num = $num / $base**($rank <= 0 && abs($num) < 1 ? $rank-1 : $rank);
if ($opts->{return_array}) {
return [$num, $prefix];
} else {
my $snum = sprintf("%.${prec}f", $num);
return $snum . $prefix;
}
}
1;
# ABSTRACT: Number utilities
__END__
=pod
=encoding utf-8
=head1 NAME
SHARYANTO::Number::Util - Number utilities
=head1 VERSION
version 0.53
=head1 SYNOPSIS
=head1 FUNCTIONS
=head2 format_metric($num, \%opts) => STR
Format C<$num> using metric prefix, e.g.:
format_metric(14 , {base=>10}); # => "14"
format_metric(12000 , {base=> 2, precision=>1}); # => "11.7K"
format_metric(12000 , {base=>10, precision=>1}); # => "11.7Ki"
format_metric(-0.0017, {base=>10}); # => "1.7m"
Known options:
=over
=item * base => INT (either 2 or 10, default: 2)
=item * precision => INT
=item * i_mark => BOOL (default: 1)
Give "i" suffix to prefixes when in base 10 for K, M, G, T, and so on.
=back
None are exported by default, but they are exportable.
=head1 SEE ALSO
Number formatting routines: L<Number::Format>, L<Format::Human::Bytes>,
L<Number::Bytes::Human>.
https://en.wikipedia.org/wiki/Metric_prefix
=head1 AUTHOR
Steven Haryanto <stevenharyanto@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2013 by Steven Haryanto.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DESCRIPTION
=cut
|