| 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
| |||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$args->{"style"} //= "general"
$args->{"style"}
Truth table100%2/2
A
A= $args->{"style"}
| |||||||||||||||||||||||||||||
| 100 | 13 | ▶ | $args->{style} = 'general' if !$styles{$args->{style}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 13 | ▶ | $args->{prefix} //= ""; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"prefix"} //= ""
MC/DC100%1/1
$args->{"prefix"} //= ""
$args->{"prefix"}
Truth table100%2/2
A
A= $args->{"prefix"}
| |||||||||||||||||||||||||||||
| 103 | 13 | ▶ | $args->{suffix} //= ""; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"suffix"} //= ""
MC/DC100%1/1
$args->{"suffix"} //= ""
$args->{"suffix"}
Truth table100%2/2
A
A= $args->{"suffix"}
| |||||||||||||||||||||||||||||
| 104 | $args->{decimal_digits} //= | ||||||||||||||||||||||||||||
| 105 | 13 | ▶ | ($args->{style} eq 'kilo' || $args->{style} eq 'kibi' ? 1 : 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
$args->{"decimal_digits"} //= $args->{"style"} eq "kilo" || $args->{"style"} eq "kibi" ? 1 : 2
$args->{"style"} eq "kilo" or $args->{"style"} eq "kibi"
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"}
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 13 | my $orig_locale = setlocale(LC_ALL); | |||||||||||||||||||||||||||
| 108 | 13 | ▶ | if ($ENV{LC_NUMERIC}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $item and &looks_like_number($item)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||