-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagnitude.pl
50 lines (43 loc) · 881 Bytes
/
magnitude.pl
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
#!/usr/bin/perl
use strict;
use warnings;
my $bignum = 7893420000;
my $nice_form = number_suffix($bignum);
print "$bignum = $nice_form\n";
$bignum = 264340000000;
print "$bignum = ", number_suffix($bignum), "\n";
$bignum = 3460000000000;
number_suffix($bignum);
sub number_suffix
{
unless ( @_ )
{
die "number_suffix needs an argument\n";
}
my $n = shift;
my $suffix = '';
my @threshold = ( K => 1E3, M => 1E6, G => 1E9, T => 1E12);
while (my $divisor = pop @threshold )
{
my $l = pop @threshold;
unless ( $n >= $divisor )
{
next;
}
$n /= $divisor;
$suffix = $l;
last;
}
if ( wantarray )
{
return ($n, $suffix);
}
elsif ( defined(wantarray) )
{
return "$n $suffix";
}
else
{
print "$n $suffix\n";
}
}