Human.pm

filter: Statementstmt 100% 3 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 4 / 4 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« lln.pl ↑ Summary
1 package Number::Finance::Human;
2 3 ▶use parent qw(Exporter autobox);
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 our @EXPORT = qw();
5 our @EXPORT_OK = qw(to_number to_human);
6 our %EXPORT_TAGS = (all => \@EXPORT_OK, autobox => 1);
7  
8 use Scalar::Util qw(looks_like_number blessed);
9 use strict;
10  
11 $\ = "\n"; $, = "\t";
12  
13 # ABSTRACT: human-readable numbers for accounting, finance and so on
14  
15  
16 my $precision = 2;
17  
18 my $suffixes = {
19 "k" => 1000,
20 "M" => 1000 * 1000,
21 "B" => 1000 * 1000 * 1000,
22 "" => 1,
23 "%" => 0.01,
24 "c" => 0.01,
25 };
26  
27 sub import {
28 my $class = shift;
29 # print @_;
30 autobox->import( SCALAR => 'Number::Finance::Human::autobox') if grep { /^:autobox$/ } @_;
31 $class->export_to_level(1, $class, @_);
32 }
33  
34 sub new {
35 my $class = shift;
36 my $s = shift();
37 my $n = looks_like_number $s ?
38 $s :
39 to_number($s)
40 ;
41 return bless [ $n, $s ], $class;
42 }
43  
44 sub to_number {
45 local $_ = shift;
46 return $_->[0] if blessed $_;
47  
48 my ($n, $s, $e) = /(.+?)([a-zA-Z%])(.*)/;
49 return $n * ($suffixes->{$s} || $suffixes->{lc $s} || $suffixes->{uc $s});
50 }
51  
52 sub to_human {
53 my $number = shift;
54  
55 return $number->[1] if blessed $number && $number->[1] =~ /\D/;
56  
57 $number = $number->[0] if blessed $number;
58  
59 my $precision = (defined $_[0]) ? shift : $precision;
60  
61 for (reverse sort { $suffixes->{$a} <=> $suffixes->{$b} } grep { /[a-z]/i || /^$/ } keys $suffixes->%*) {
62 return sprintf "%0.${precision}f%s", $number / $suffixes->{$_}, $_
63 if $number >= $suffixes->{$_}
64 }
65 }
66  
67 *to_string = *to_human;
68  
69 # -------------------------------------------
70  
71 use overload
72 '0+' => \&to_number,
73 '""' => \&to_human,
74 '+' => 'op_add',
75 '-' => 'op_sub',
76 '*' => 'op_mul',
77 '/' => 'op_div',
78 fallback => 1
79 ;
80  
81 sub op_add {
82 my ($va, $vb, $swap) = @_;
83 $vb = ref $vb && $vb->isa(__PACKAGE__) ? $vb : __PACKAGE__->new($vb);
84 return __PACKAGE__->new($va->to_number + $vb->to_number);
85 }
86  
87 sub op_mul {
88 my ($va, $vb, $swap) = @_;
89 $vb = ref $vb && $vb->isa(__PACKAGE__) ? $vb : __PACKAGE__->new($vb);
90 return __PACKAGE__->new($va->to_number * $vb->to_number);
91 }
92  
93 sub op_div {
94 my ($va, $vb, $swap) = @_;
95 $vb = ref $vb && $vb->isa(__PACKAGE__) ? $vb : __PACKAGE__->new($vb);
96  
97 return $swap ?
98 __PACKAGE__->new($vb->to_number / $va->to_number) :
99 __PACKAGE__->new($va->to_number / $vb->to_number);
100 }
101  
102 sub op_sub {
103 my ($va, $vb, $swap) = @_;
104 $vb = ref $vb && $vb->isa(__PACKAGE__) ? $vb : __PACKAGE__->new($vb);
105  
106 return $swap ?
107 __PACKAGE__->new($vb->to_number - $va->to_number) :
108 __PACKAGE__->new($va->to_number - $vb->to_number);
109 }
110  
111 package Number::Finance::Human::autobox;
112  
113 sub to_nfh { Number::Finance::Human->new(shift) }
114  
115 sub to_human { Number::Finance::Human->new(shift)->to_human }
116  
117 sub to_number { Number::Finance::Human->new(shift)->to_number }
118  
119 1;
120  
121 =encoding UTF-8
122  
123 =pod
124  
125 =head1 NAME
126  
127 Number::Finance::Human - Human-readable numbers for accounting and finance
128  
129 =head1 SYNOPSIS
130  
131 use Number::Finance::Human qw(to_number to_human);
132  
133 my $n = to_number("2.5M"); # 2500000
134 my $h = to_human(1500); # "1.50k"
135  
136 my $x = Number::Finance::Human->new("3k");
137 say $x + 500; # "3.50k"
138  
139 use Number::Finance::Human ':autobox';
140 say 2500000->to_human; # "2.50M"
141  
142 =head1 DESCRIPTION
143  
144 This module converts between numeric values and human-readable
145 representations commonly used in finance and accounting.
146  
147 It supports suffixes such as C<k>, C<M>, C<B>, C<%>, and C<c>, and provides
148 operator overloading so objects behave like numbers while stringifying to
149 human-readable form.
150  
151 =head1 SUFFIXES
152  
153 The following suffixes are recognized:
154  
155 k 1_000
156 M 1_000_000
157 B 1_000_000_000
158 % 0.01
159 c 0.01
160 (empty) 1
161  
162 Suffix matching is case-insensitive.
163  
164 =head1 FUNCTIONS
165  
166 =head2 to_number($value)
167  
168 Converts a human-readable string (e.g. C<"2.5M">, C<"10%">) into a numeric
169 value.
170  
171 If passed a C<Number::Finance::Human> object, returns its numeric value.
172  
173 =head2 to_human($number [, $precision ])
174  
175 Formats a numeric value as a human-readable string using the largest
176 applicable suffix.
177  
178 An optional precision (default: 2) controls the number of decimal places.
179  
180 If called on an object that was constructed from a non-numeric string, the
181 original string is preserved.
182  
183 =head1 METHODS
184  
185 =head2 new($value)
186  
187 Creates a new C<Number::Finance::Human> object from either a numeric value
188 or a human-readable string.
189  
190 =head2 to_number
191  
192 Returns the numeric value of the object.
193  
194 =head2 to_human
195  
196 Returns the human-readable string representation.
197  
198 =head2 to_string
199  
200 Alias for C<to_human>.
201  
202 =head1 OPERATOR OVERLOADING
203  
204 Objects overload the following operators:
205  
206 0+ numeric context
207 "" string context (human-readable)
208 + - * /
209  
210 Arithmetic between objects or scalars returns a new
211 C<Number::Finance::Human> object.
212  
213 =head1 AUTOBOXING
214  
215 When imported with C<:autobox>, scalar values gain the following methods:
216  
217 to_nfh
218 to_number
219 to_human
220  
221 Example:
222  
223 use Number::Finance::Human ':autobox';
224 say 1200000->to_human; # "1.20M"
225  
226 =head1 EXPORTS
227  
228 Nothing is exported by default.
229  
230 Optional exports:
231  
232 to_number
233 to_human
234  
235 Tag:
236  
237 :all exports all functions
238 :autobox enables autoboxing
239  
240 =head1 SEE ALSO
241  
242 L<autobox>, L<Scalar::Util>
243  
244 =head1 AUTHOR
245  
246 Simone Cesano <scesano@cpan.org>
247  
248 =head1 LICENSE
249  
250 This software is copyright (c) 2025 by Simone Cesano.
251  
252 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
« lln.pl ↑ Summary