Data/Unixish/num.pm

filter: Statementstmt 97.5% 78 / 80 Branchbran 76.9% 20 / 26 Conditioncond 100% 14 / 14 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 94.3% 133 / 141 CC 20 SCAR 19.8
CC
20
Cov
93%
_num_begin
13.2
_num_item
8.0
num
2.0
CRAP 7.2
« Data/Unixish/sort.pm ↑ Summary Data/Unixish/sprintfn.pm »
1 package Data::Unixish::num;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use syntax 'each_on_array'; # to support perl < 5.12
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #use Log::Any '$log';
9  
10 2 ▶use Data::Unixish::Util qw(%common_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Number::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Number::Format::Metric qw(format_metric);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use POSIX qw(locale_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
17 2 our $DATE = '2025-02-24'; # DATE
18 2 our $DIST = 'Data-Unixish'; # DIST
19 2 our $VERSION = '1.574'; # VERSION
20  
21 2 our %SPEC;
22  
23 2 my %styles = (
24 general => 'General formatting, e.g. 1, 2.345',
25 fixed => 'Fixed number of decimal digits, e.g. 1.00, default decimal digits=2',
26 scientific => 'Scientific notation, e.g. 1.23e+21',
27 kilo => 'Use K/M/G/etc suffix with base-2, e.g. 1.2M',
28 kibi => 'Use Ki/Mi/GiB/etc suffix with base-10 [1000], e.g. 1.2Mi',
29 percent => 'Percentage, e.g. 10.00%',
30 # XXX fraction
31 # XXX currency?
32 );
33  
34 # XXX negative number -X or (X)
35 # XXX colorize negative number?
36 # XXX leading zeros/spaces
37  
38 $SPEC{num} = {
39 v => 1.1,
40 summary => 'Format number',
41 description => <<'MARKDOWN',
42  
43 Observe locale environment variable settings.
44  
45 Undef and non-numbers are ignored.
46  
47 MARKDOWN
48 args => {
49 %common_args,
50 style => {
51 schema=>['str*', in=>[keys %styles], default=>'general'],
52 cmdline_aliases => { s=>{} },
53 pos => 0,
54 description => "Available styles:\n\n".
55 2 join("", map {" * $_ ($styles{$_})\n"} sort keys %styles),
56 },
57 decimal_digits => {
58 schema => ['int*'],
59 summary => 'Number of digits to the right of decimal point',
60 },
61 thousands_sep => {
62 summary => 'Use a custom thousand separator character',
63 description => <<'MARKDOWN',
64  
65 Default is from locale (e.g. dot "." for en_US, etc).
66  
67 Use empty string "" if you want to disable printing thousands separator.
68  
69 MARKDOWN
70 schema => ['str*'],
71 },
72 prefix => {
73 summary => 'Add some string at the beginning (e.g. for currency)',
74 schema => ['str*'],
75 },
76 suffix => {
77 summary => 'Add some string at the end (e.g. for unit)',
78 schema => ['str*'],
79 },
80 },
81 tags => [qw/formatting itemfunc datatype:num/],
82 };
83 7 ▶sub num {
Subroutine num: called CC=2 SCAR=6.9
Pod documented
84 7 my %args = @_;
85 7 my ($in, $out) = ($args{in}, $args{out});
86  
87 7 my $orig_locale = _num_begin(\%args);
88 7 ▶ while (my ($index, $item) = each @$in) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$in) 41 7 48
89 41 push @$out, _num_item($item, \%args);
90 }
91 7 _num_end(\%args, $orig_locale);
92  
93 7 [200, "OK"];
94 }
95  
96 13 ▶sub _num_begin {
Subroutine _num_begin: called CC=12 SCAR=25.8
97 13 my $args = shift;
98  
99 13 ▶ $args->{style} //= 'general';
Condition100%2/2
$args->{"style"} //= "general"
l!l
94
MC/DC100%1/1
$args->{"style"} //= "general"
$args->{"style"}
Truth table100%2/2
A
A= $args->{"style"}
Aresult
11
00
100 13 ▶ $args->{style} = 'general' if !$styles{$args->{style}};
Branch: 1 branch
BranchTrueFalseTotal
unless $styles{$args->{"style"}} 0 13 13
101  
102 13 ▶ $args->{prefix} //= "";
Condition100%2/2
$args->{"prefix"} //= ""
l!l
211
MC/DC100%1/1
$args->{"prefix"} //= ""
$args->{"prefix"}
Truth table100%2/2
A
A= $args->{"prefix"}
Aresult
11
00
103 13 ▶ $args->{suffix} //= "";
Condition100%2/2
$args->{"suffix"} //= ""
l!l
211
MC/DC100%1/1
$args->{"suffix"} //= ""
$args->{"suffix"}
Truth table100%2/2
A
A= $args->{"suffix"}
Aresult
11
00
104 $args->{decimal_digits} //=
105 13 ▶ ($args->{style} eq 'kilo' || $args->{style} eq 'kibi' ? 1 : 2);
Branch: 1 branch
BranchTrueFalseTotal
$args->{'style'} eq 'kilo' || $args->{'style'} eq 'kibi' ? : 4 6 10
Condition100%5/5
$args->{"decimal_digits"} //= $args->{"style"} eq "kilo" || $args->{"style"} eq "kibi" ? 1 : 2
l!l
310
$args->{"style"} eq "kilo" or $args->{"style"} eq "kibi"
l!l&&r!l&&!r
226
MC/DC100%1/1
$args->{"decimal_digits"} //= $args->{"style"} eq "kilo" || $args->{"style"} eq "kibi" ? 1 : 2
$args->{"decimal_digits"}
Truth table100%2/2
A
A= $args->{"decimal_digits"}
Aresult
11
00
106  
107 13 my $orig_locale = setlocale(LC_ALL);
108 13 ▶ if ($ENV{LC_NUMERIC}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ENV{'LC_NUMERIC'}) { } 13 0 13
elsif ($ENV{'LC_ALL'}) { } 0 0 0
elsif ($ENV{'LANG'}) { } 0 0 0
109 13 setlocale(LC_NUMERIC, $ENV{LC_NUMERIC});
110 } elsif ($ENV{LC_ALL}) {
111 0 setlocale(LC_ALL, $ENV{LC_ALL});
112 } elsif ($ENV{LANG}) {
113 0 setlocale(LC_ALL, $ENV{LANG});
114 }
115  
116 # args abused to store object/state
117 13 my %nfargs;
118 13 ▶ if (defined $args->{thousands_sep}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"thousands_sep"}) 2 11 13
119 2 $nfargs{THOUSANDS_SEP} = $args->{thousands_sep};
120 }
121 13 $args->{_nf} = Number::Format->new(%nfargs);
122  
123 13 return $orig_locale;
124 }
125  
126 75 ▶sub _num_item {
Subroutine _num_item: called CC=8 SCAR=20.8
127 75 my ($item, $args) = @_;
128  
129 {
130 75 ▶ last if !defined($item) || !looks_like_number($item);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item and &looks_like_number($item) 44 31 75
Condition100%3/3
defined $item and &looks_like_number($item)
!ll&&!rl&&r
113331
MC/DC100%2/2
defined $item and &looks_like_number($item)
defined $item &looks_like_number($item)
Truth table100%3/3
A and B
A= defined $itemB= &looks_like_number($item)
ABresult
0X0
100
111
131 31 my $nf = $args->{_nf};
132 31 my $style = $args->{style};
133 31 my $decdigs = $args->{decimal_digits};
134  
135 31 ▶ if ($style eq 'fixed') {
Branch: 5 branches
BranchTrueFalseTotal
if ($style eq 'fixed') { } 6 25 31
elsif ($style eq 'scientific') { } 3 22 25
elsif ($style eq 'kilo') { } 6 16 22
elsif ($style eq 'kibi') { } 6 10 16
elsif ($style eq 'percent') { } 6 4 10
136 6 $item = $nf->format_number($item, $decdigs, $decdigs);
137 } elsif ($style eq 'scientific') {
138 3 $item = sprintf("%.${decdigs}e", $item);
139 } elsif ($style eq 'kilo') {
140 6 my $res = format_metric($item, {base=>2, return_array=>1});
141 6 $item = $nf->format_number($res->[0], $decdigs, $decdigs) .
142 $res->[1];
143 } elsif ($style eq 'kibi') {
144 6 my $res = format_metric(
145 $item, {base=>10, return_array=>1});
146 6 $item = $nf->format_number($res->[0], $decdigs, $decdigs) .
147 $res->[1];
148 } elsif ($style eq 'percent') {
149 6 $item = sprintf("%.${decdigs}f%%", $item*100);
150 } else {
151 # general
152 4 $item = $nf->format_number($item);
153 }
154 31 $item = "$args->{prefix}$item$args->{suffix}";
155 }
156 75 return $item;
157 }
158  
159 13 ▶sub _num_end {
Subroutine _num_end: called CC=1 SCAR=0.0
160 13 my ($args, $orig_locale) = @_;
161 13 setlocale(LC_ALL, $orig_locale);
162 }
163  
164 2 1;
165 # ABSTRACT: Format number
166  
167 __END__
« Data/Unixish/sort.pm ↑ Summary Data/Unixish/sprintfn.pm »