line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Number::Format; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Minimum version is 5.8.0. May work on earlier versions, but not |
4
|
|
|
|
|
|
|
# supported on any version older than 5.8. |
5
|
|
|
|
|
|
|
require 5.008; |
6
|
|
|
|
|
|
|
|
7
|
9
|
|
|
9
|
|
334048
|
use strict; |
|
9
|
|
|
|
|
23
|
|
|
9
|
|
|
|
|
350
|
|
8
|
9
|
|
|
9
|
|
48
|
use warnings; |
|
9
|
|
|
|
|
2839
|
|
|
9
|
|
|
|
|
617
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Number::Format - Perl extension for formatting numbers |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use Number::Format; |
17
|
|
|
|
|
|
|
my $x = new Number::Format %args; |
18
|
|
|
|
|
|
|
$formatted = $x->round($number, $precision); |
19
|
|
|
|
|
|
|
$formatted = $x->format_number($number, $precision, $trailing_zeroes); |
20
|
|
|
|
|
|
|
$formatted = $x->format_negative($number, $picture); |
21
|
|
|
|
|
|
|
$formatted = $x->format_picture($number, $picture); |
22
|
|
|
|
|
|
|
$formatted = $x->format_price($number, $precision, $symbol); |
23
|
|
|
|
|
|
|
$formatted = $x->format_bytes($number, $precision); |
24
|
|
|
|
|
|
|
$number = $x->unformat_number($formatted); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
use Number::Format qw(:subs); |
27
|
|
|
|
|
|
|
$formatted = round($number, $precision); |
28
|
|
|
|
|
|
|
$formatted = format_number($number, $precision, $trailing_zeroes); |
29
|
|
|
|
|
|
|
$formatted = format_negative($number, $picture); |
30
|
|
|
|
|
|
|
$formatted = format_picture($number, $picture); |
31
|
|
|
|
|
|
|
$formatted = format_price($number, $precision, $symbol); |
32
|
|
|
|
|
|
|
$formatted = format_bytes($number, $precision); |
33
|
|
|
|
|
|
|
$number = unformat_number($formatted); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 REQUIRES |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Perl, version 5.8 or higher. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
POSIX.pm to determine locale settings. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Carp.pm is used for some error reporting. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 DESCRIPTION |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
These functions provide an easy means of formatting numbers in a |
46
|
|
|
|
|
|
|
manner suitable for displaying to the user. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
There are two ways to use this package. One is to declare an object |
49
|
|
|
|
|
|
|
of type Number::Format, which you can think of as a formatting engine. |
50
|
|
|
|
|
|
|
The various functions defined here are provided as object methods. |
51
|
|
|
|
|
|
|
The constructor C can be used to set the parameters of the |
52
|
|
|
|
|
|
|
formatting engine. Valid parameters are: |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
THOUSANDS_SEP - character inserted between groups of 3 digits |
55
|
|
|
|
|
|
|
DECIMAL_POINT - character separating integer and fractional parts |
56
|
|
|
|
|
|
|
MON_THOUSANDS_SEP - like THOUSANDS_SEP, but used for format_price |
57
|
|
|
|
|
|
|
MON_DECIMAL_POINT - like DECIMAL_POINT, but used for format_price |
58
|
|
|
|
|
|
|
INT_CURR_SYMBOL - character(s) denoting currency (see format_price()) |
59
|
|
|
|
|
|
|
DECIMAL_DIGITS - number of digits to the right of dec point (def 2) |
60
|
|
|
|
|
|
|
DECIMAL_FILL - boolean; whether to add zeroes to fill out decimal |
61
|
|
|
|
|
|
|
NEG_FORMAT - format to display negative numbers (def ``-x'') |
62
|
|
|
|
|
|
|
KILO_SUFFIX - suffix to add when format_bytes formats kilobytes (trad) |
63
|
|
|
|
|
|
|
MEGA_SUFFIX - " " " " " " megabytes (trad) |
64
|
|
|
|
|
|
|
GIGA_SUFFIX - " " " " " " gigabytes (trad) |
65
|
|
|
|
|
|
|
KIBI_SUFFIX - suffix to add when format_bytes formats kibibytes (iec) |
66
|
|
|
|
|
|
|
MEBI_SUFFIX - " " " " " " mebibytes (iec) |
67
|
|
|
|
|
|
|
GIBI_SUFFIX - " " " " " " gibibytes (iec) |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
They may be specified in upper or lower case, with or without a |
70
|
|
|
|
|
|
|
leading hyphen ( - ). |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
If C is set to the empty string, format_number will not |
73
|
|
|
|
|
|
|
insert any separators. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
The defaults for C, C, |
76
|
|
|
|
|
|
|
C, C, and C |
77
|
|
|
|
|
|
|
come from the POSIX locale information (see L). If your |
78
|
|
|
|
|
|
|
POSIX locale does not provide C and/or |
79
|
|
|
|
|
|
|
C fields, then the C and/or |
80
|
|
|
|
|
|
|
C values are used for those parameters. Formerly, |
81
|
|
|
|
|
|
|
POSIX was optional but this caused problems in some cases, so it is |
82
|
|
|
|
|
|
|
now required. If this causes you hardship, please contact the author |
83
|
|
|
|
|
|
|
of this package at (remove "SPAM" to get correct |
84
|
|
|
|
|
|
|
email address) for help. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
If any of the above parameters are not specified when you invoke |
87
|
|
|
|
|
|
|
C, then the values are taken from package global variables of |
88
|
|
|
|
|
|
|
the same name (e.g. C<$DECIMAL_POINT> is the default for the |
89
|
|
|
|
|
|
|
C parameter). If you use the C<:vars> keyword on your |
90
|
|
|
|
|
|
|
C |
91
|
|
|
|
|
|
|
will import those variables into your namesapce and can assign values |
92
|
|
|
|
|
|
|
as if they were your own local variables. The default values for all |
93
|
|
|
|
|
|
|
the parameters are: |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
THOUSANDS_SEP = ',' |
96
|
|
|
|
|
|
|
DECIMAL_POINT = '.' |
97
|
|
|
|
|
|
|
MON_THOUSANDS_SEP = ',' |
98
|
|
|
|
|
|
|
MON_DECIMAL_POINT = '.' |
99
|
|
|
|
|
|
|
INT_CURR_SYMBOL = 'USD' |
100
|
|
|
|
|
|
|
DECIMAL_DIGITS = 2 |
101
|
|
|
|
|
|
|
DECIMAL_FILL = 0 |
102
|
|
|
|
|
|
|
NEG_FORMAT = '-x' |
103
|
|
|
|
|
|
|
KILO_SUFFIX = 'K' |
104
|
|
|
|
|
|
|
MEGA_SUFFIX = 'M' |
105
|
|
|
|
|
|
|
GIGA_SUFFIX = 'G' |
106
|
|
|
|
|
|
|
KIBI_SUFFIX = 'KiB' |
107
|
|
|
|
|
|
|
MEBI_SUFFIX = 'MiB' |
108
|
|
|
|
|
|
|
GIBI_SUFFIX = 'GiB' |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Note however that when you first call one of the functions in this |
111
|
|
|
|
|
|
|
module I using the object-oriented interface, further setting |
112
|
|
|
|
|
|
|
of those global variables will have no effect on non-OO calls. It is |
113
|
|
|
|
|
|
|
recommended that you use the object-oriented interface instead for |
114
|
|
|
|
|
|
|
fewer headaches and a cleaner design. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
The C and C values are not set by the |
117
|
|
|
|
|
|
|
Locale system, but are definable by the user. They affect the output |
118
|
|
|
|
|
|
|
of C. Setting C is like giving that |
119
|
|
|
|
|
|
|
value as the C<$precision> argument to that function. Setting |
120
|
|
|
|
|
|
|
C to a true value causes C to append |
121
|
|
|
|
|
|
|
zeroes to the right of the decimal digits until the length is the |
122
|
|
|
|
|
|
|
specified number of digits. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
C is only used by C and is a string |
125
|
|
|
|
|
|
|
containing the letter 'x', where that letter will be replaced by a |
126
|
|
|
|
|
|
|
positive representation of the number being passed to that function. |
127
|
|
|
|
|
|
|
C and C utilize this feature by |
128
|
|
|
|
|
|
|
calling C if the number was less than 0. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
C, C, and C are used by |
131
|
|
|
|
|
|
|
C when the value is over 1024, 1024*1024, or |
132
|
|
|
|
|
|
|
1024*1024*1024, respectively. The default values are "K", "M", and |
133
|
|
|
|
|
|
|
"G". These apply in the default "traditional" mode only. Note: TERA |
134
|
|
|
|
|
|
|
or higher are not implemented because of integer overflows on 32-bit |
135
|
|
|
|
|
|
|
systems. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
C, C, and C are used by |
138
|
|
|
|
|
|
|
C when the value is over 1024, 1024*1024, or |
139
|
|
|
|
|
|
|
1024*1024*1024, respectively. The default values are "KiB", "MiB", |
140
|
|
|
|
|
|
|
and "GiB". These apply in the "iso60027"" mode only. Note: TEBI or |
141
|
|
|
|
|
|
|
higher are not implemented because of integer overflows on 32-bit |
142
|
|
|
|
|
|
|
systems. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
The only restrictions on C and C are that |
145
|
|
|
|
|
|
|
they must not be digits, must not be identical, and must each be one |
146
|
|
|
|
|
|
|
character. There are no restrictions on C. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
For example, a German user might include this in their code: |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
use Number::Format; |
151
|
|
|
|
|
|
|
my $de = new Number::Format(-thousands_sep => '.', |
152
|
|
|
|
|
|
|
-decimal_point => ',', |
153
|
|
|
|
|
|
|
-int_curr_symbol => 'DEM'); |
154
|
|
|
|
|
|
|
my $formatted = $de->format_number($number); |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Or, if you prefer not to use the object oriented interface, you can do |
157
|
|
|
|
|
|
|
this instead: |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
use Number::Format qw(:subs :vars); |
160
|
|
|
|
|
|
|
$THOUSANDS_SEP = '.'; |
161
|
|
|
|
|
|
|
$DECIMAL_POINT = ','; |
162
|
|
|
|
|
|
|
$INT_CURR_SYMBOL = 'DEM'; |
163
|
|
|
|
|
|
|
my $formatted = format_number($number); |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 EXPORTS |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Nothing is exported by default. To export the functions or the global |
168
|
|
|
|
|
|
|
variables defined herein, specify the function name(s) on the import |
169
|
|
|
|
|
|
|
list of the C |
170
|
|
|
|
|
|
|
defined herein, use the special tag C<:subs>. To export the |
171
|
|
|
|
|
|
|
variables, use the special tag C<:vars>; to export both subs and vars |
172
|
|
|
|
|
|
|
you can use the tag C<:all>. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
###--------------------------------------------------------------------- |
177
|
|
|
|
|
|
|
|
178
|
9
|
|
|
9
|
|
46
|
use strict; |
|
9
|
|
|
|
|
28
|
|
|
9
|
|
|
|
|
234
|
|
179
|
9
|
|
|
9
|
|
43
|
use Exporter; |
|
9
|
|
|
|
|
21
|
|
|
9
|
|
|
|
|
357
|
|
180
|
9
|
|
|
9
|
|
56
|
use Carp; |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
656
|
|
181
|
9
|
|
|
9
|
|
2121
|
use POSIX; |
|
9
|
|
|
|
|
15873
|
|
|
9
|
|
|
|
|
53
|
|
182
|
9
|
|
|
9
|
|
23502
|
use base qw(Exporter); |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
15026
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
our @EXPORT_SUBS = |
185
|
|
|
|
|
|
|
qw( format_number format_negative format_picture |
186
|
|
|
|
|
|
|
format_price format_bytes round unformat_number ); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
our @EXPORT_LC_NUMERIC = |
189
|
|
|
|
|
|
|
qw( $DECIMAL_POINT $THOUSANDS_SEP $GROUPING ); |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
our @EXPORT_LC_MONETARY = |
192
|
|
|
|
|
|
|
qw( $INT_CURR_SYMBOL $CURRENCY_SYMBOL $MON_DECIMAL_POINT |
193
|
|
|
|
|
|
|
$MON_THOUSANDS_SEP $MON_GROUPING $POSITIVE_SIGN $NEGATIVE_SIGN |
194
|
|
|
|
|
|
|
$INT_FRAC_DIGITS $FRAC_DIGITS $P_CS_PRECEDES $P_SEP_BY_SPACE |
195
|
|
|
|
|
|
|
$N_CS_PRECEDES $N_SEP_BY_SPACE $P_SIGN_POSN $N_SIGN_POSN ); |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
our @EXPORT_OTHER = |
198
|
|
|
|
|
|
|
qw( $DECIMAL_DIGITS $DECIMAL_FILL $NEG_FORMAT |
199
|
|
|
|
|
|
|
$KILO_SUFFIX $MEGA_SUFFIX $GIGA_SUFFIX |
200
|
|
|
|
|
|
|
$KIBI_SUFFIX $MEBI_SUFFIX $GIBI_SUFFIX ); |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
our @EXPORT_VARS = ( @EXPORT_LC_NUMERIC, @EXPORT_LC_MONETARY, @EXPORT_OTHER ); |
203
|
|
|
|
|
|
|
our @EXPORT_ALL = ( @EXPORT_SUBS, @EXPORT_VARS ); |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
our @EXPORT_OK = ( @EXPORT_ALL ); |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( subs => \@EXPORT_SUBS, |
208
|
|
|
|
|
|
|
vars => \@EXPORT_VARS, |
209
|
|
|
|
|
|
|
lc_numeric_vars => \@EXPORT_LC_NUMERIC, |
210
|
|
|
|
|
|
|
lc_monetary_vars => \@EXPORT_LC_MONETARY, |
211
|
|
|
|
|
|
|
other_vars => \@EXPORT_OTHER, |
212
|
|
|
|
|
|
|
all => \@EXPORT_ALL ); |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
our $VERSION = '1.73'; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Refer to http://www.opengroup.org/onlinepubs/007908775/xbd/locale.html |
217
|
|
|
|
|
|
|
# for more details about the POSIX variables |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# Locale variables provided by POSIX for numbers (LC_NUMERIC) |
220
|
|
|
|
|
|
|
our $DECIMAL_POINT = '.'; # decimal point symbol for numbers |
221
|
|
|
|
|
|
|
our $THOUSANDS_SEP = ','; # thousands separator for numbers |
222
|
|
|
|
|
|
|
our $GROUPING = undef;# grouping rules for thousands (UNSUPPORTED) |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
# Locale variables provided by POSIX for currency (LC_MONETARY) |
225
|
|
|
|
|
|
|
our $INT_CURR_SYMBOL = 'USD';# intl currency symbol |
226
|
|
|
|
|
|
|
our $CURRENCY_SYMBOL = '$'; # domestic currency symbol |
227
|
|
|
|
|
|
|
our $MON_DECIMAL_POINT = '.'; # decimal point symbol for monetary values |
228
|
|
|
|
|
|
|
our $MON_THOUSANDS_SEP = ','; # thousands separator for monetary values |
229
|
|
|
|
|
|
|
our $MON_GROUPING = undef;# like 'grouping' for monetary (UNSUPPORTED) |
230
|
|
|
|
|
|
|
our $POSITIVE_SIGN = ''; # string to add for non-negative monetary |
231
|
|
|
|
|
|
|
our $NEGATIVE_SIGN = '-'; # string to add for negative monetary |
232
|
|
|
|
|
|
|
our $INT_FRAC_DIGITS = 2; # digits to right of decimal for intl currency |
233
|
|
|
|
|
|
|
our $FRAC_DIGITS = 2; # digits to right of decimal for currency |
234
|
|
|
|
|
|
|
our $P_CS_PRECEDES = 1; # curr sym precedes(1) or follows(0) positive |
235
|
|
|
|
|
|
|
our $P_SEP_BY_SPACE = 1; # add space to positive; 0, 1, or 2 |
236
|
|
|
|
|
|
|
our $N_CS_PRECEDES = 1; # curr sym precedes(1) or follows(0) negative |
237
|
|
|
|
|
|
|
our $N_SEP_BY_SPACE = 1; # add space to negative; 0, 1, or 2 |
238
|
|
|
|
|
|
|
our $P_SIGN_POSN = 1; # sign rules for positive: 0-4 |
239
|
|
|
|
|
|
|
our $N_SIGN_POSN = 1; # sign rules for negative: 0-4 |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# The following are specific to Number::Format |
242
|
|
|
|
|
|
|
our $DECIMAL_DIGITS = 2; |
243
|
|
|
|
|
|
|
our $DECIMAL_FILL = 0; |
244
|
|
|
|
|
|
|
our $NEG_FORMAT = '-x'; |
245
|
|
|
|
|
|
|
our $KILO_SUFFIX = 'K'; |
246
|
|
|
|
|
|
|
our $MEGA_SUFFIX = 'M'; |
247
|
|
|
|
|
|
|
our $GIGA_SUFFIX = 'G'; |
248
|
|
|
|
|
|
|
our $KIBI_SUFFIX = 'KiB'; |
249
|
|
|
|
|
|
|
our $MEBI_SUFFIX = 'MiB'; |
250
|
|
|
|
|
|
|
our $GIBI_SUFFIX = 'GiB'; |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
our $DEFAULT_LOCALE = { ( |
253
|
|
|
|
|
|
|
# LC_NUMERIC |
254
|
|
|
|
|
|
|
decimal_point => $DECIMAL_POINT, |
255
|
|
|
|
|
|
|
thousands_sep => $THOUSANDS_SEP, |
256
|
|
|
|
|
|
|
grouping => $GROUPING, |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
# LC_MONETARY |
259
|
|
|
|
|
|
|
int_curr_symbol => $INT_CURR_SYMBOL, |
260
|
|
|
|
|
|
|
currency_symbol => $CURRENCY_SYMBOL, |
261
|
|
|
|
|
|
|
mon_decimal_point => $MON_DECIMAL_POINT, |
262
|
|
|
|
|
|
|
mon_thousands_sep => $MON_THOUSANDS_SEP, |
263
|
|
|
|
|
|
|
mon_grouping => $MON_GROUPING, |
264
|
|
|
|
|
|
|
positive_sign => $POSITIVE_SIGN, |
265
|
|
|
|
|
|
|
negative_sign => $NEGATIVE_SIGN, |
266
|
|
|
|
|
|
|
int_frac_digits => $INT_FRAC_DIGITS, |
267
|
|
|
|
|
|
|
frac_digits => $FRAC_DIGITS, |
268
|
|
|
|
|
|
|
p_cs_precedes => $P_CS_PRECEDES, |
269
|
|
|
|
|
|
|
p_sep_by_space => $P_SEP_BY_SPACE, |
270
|
|
|
|
|
|
|
n_cs_precedes => $N_CS_PRECEDES, |
271
|
|
|
|
|
|
|
n_sep_by_space => $N_SEP_BY_SPACE, |
272
|
|
|
|
|
|
|
p_sign_posn => $P_SIGN_POSN, |
273
|
|
|
|
|
|
|
n_sign_posn => $N_SIGN_POSN, |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
# The following are specific to Number::Format |
276
|
|
|
|
|
|
|
decimal_digits => $DECIMAL_DIGITS, |
277
|
|
|
|
|
|
|
decimal_fill => $DECIMAL_FILL, |
278
|
|
|
|
|
|
|
neg_format => $NEG_FORMAT, |
279
|
|
|
|
|
|
|
kilo_suffix => $KILO_SUFFIX, |
280
|
|
|
|
|
|
|
mega_suffix => $MEGA_SUFFIX, |
281
|
|
|
|
|
|
|
giga_suffix => $GIGA_SUFFIX, |
282
|
|
|
|
|
|
|
kibi_suffix => $KIBI_SUFFIX, |
283
|
|
|
|
|
|
|
mebi_suffix => $MEBI_SUFFIX, |
284
|
|
|
|
|
|
|
gibi_suffix => $GIBI_SUFFIX, |
285
|
|
|
|
|
|
|
) }; |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
# |
288
|
|
|
|
|
|
|
# Largest integer a 32-bit Perl can handle is based on the mantissa |
289
|
|
|
|
|
|
|
# size of a double float, which is up to 53 bits. While we may be |
290
|
|
|
|
|
|
|
# able to support larger values on 64-bit systems, some Perl integer |
291
|
|
|
|
|
|
|
# operations on 64-bit integer systems still use the 53-bit-mantissa |
292
|
|
|
|
|
|
|
# double floats. To be safe, we cap at 2**53; use Math::BigFloat |
293
|
|
|
|
|
|
|
# instead for larger numbers. |
294
|
|
|
|
|
|
|
# |
295
|
9
|
|
|
9
|
|
112
|
use constant MAX_INT => 2**53; |
|
9
|
|
|
|
|
16
|
|
|
9
|
|
|
|
|
69293
|
|
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
###--------------------------------------------------------------------- |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
# INTERNAL FUNCTIONS |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
# These functions (with names beginning with '_' are for internal use |
302
|
|
|
|
|
|
|
# only. There is no guarantee that they will remain the same from one |
303
|
|
|
|
|
|
|
# version to the next! |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
# _get_self creates an instance of Number::Format with the default |
308
|
|
|
|
|
|
|
# values for the configuration parameters, if the first element of |
309
|
|
|
|
|
|
|
# @_ is not already an object. |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
my $DefaultObject; |
312
|
|
|
|
|
|
|
sub _get_self |
313
|
|
|
|
|
|
|
{ |
314
|
|
|
|
|
|
|
# Not calling $_[0]->isa because that may result in unblessed |
315
|
|
|
|
|
|
|
# reference error |
316
|
310
|
100
|
66
|
310
|
|
1898
|
unless (ref $_[0] && UNIVERSAL::isa($_[0], "Number::Format")) |
317
|
|
|
|
|
|
|
{ |
318
|
58
|
|
66
|
|
|
177
|
$DefaultObject ||= new Number::Format(); |
319
|
58
|
|
|
|
|
162
|
unshift (@_, $DefaultObject); |
320
|
|
|
|
|
|
|
} |
321
|
310
|
|
|
|
|
892
|
@_; |
322
|
|
|
|
|
|
|
} |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
# _check_seps is used to validate that the thousands_sep, |
327
|
|
|
|
|
|
|
# decimal_point, mon_thousands_sep and mon_decimal_point variables |
328
|
|
|
|
|
|
|
# have acceptable values. For internal use only. |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
sub _check_seps |
331
|
|
|
|
|
|
|
{ |
332
|
118
|
|
|
118
|
|
153
|
my ($self) = @_; |
333
|
118
|
50
|
|
|
|
252
|
croak "Not an object" unless ref $self; |
334
|
118
|
|
|
|
|
268
|
foreach my $prefix ("", "mon_") |
335
|
|
|
|
|
|
|
{ |
336
|
236
|
50
|
|
|
|
8150
|
croak "${prefix}thousands_sep is undefined" |
337
|
|
|
|
|
|
|
unless defined $self->{"${prefix}thousands_sep"}; |
338
|
236
|
50
|
|
|
|
729
|
croak "${prefix}thousands_sep may not be numeric" |
339
|
|
|
|
|
|
|
if $self->{"${prefix}thousands_sep"} =~ /\d/; |
340
|
236
|
50
|
|
|
|
855
|
croak "${prefix}decimal_point may not be numeric" |
341
|
|
|
|
|
|
|
if $self->{"${prefix}decimal_point"} =~ /\d/; |
342
|
236
|
50
|
|
|
|
1695
|
croak("${prefix}thousands_sep and ". |
343
|
|
|
|
|
|
|
"${prefix}decimal_point may not be equal") |
344
|
|
|
|
|
|
|
if $self->{"${prefix}decimal_point"} eq |
345
|
|
|
|
|
|
|
$self->{"${prefix}thousands_sep"}; |
346
|
|
|
|
|
|
|
} |
347
|
|
|
|
|
|
|
} |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
# _get_multipliers returns the multipliers to be used for kilo, mega, |
352
|
|
|
|
|
|
|
# and giga (un-)formatting. Used in format_bytes and unformat_number. |
353
|
|
|
|
|
|
|
# For internal use only. |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
sub _get_multipliers |
356
|
|
|
|
|
|
|
{ |
357
|
35
|
|
|
35
|
|
67
|
my($base) = @_; |
358
|
35
|
100
|
100
|
|
|
129
|
if (!defined($base) || $base == 1024) |
|
|
100
|
|
|
|
|
|
359
|
|
|
|
|
|
|
{ |
360
|
28
|
|
|
|
|
124
|
return ( kilo => 0x00000400, |
361
|
|
|
|
|
|
|
mega => 0x00100000, |
362
|
|
|
|
|
|
|
giga => 0x40000000 ); |
363
|
|
|
|
|
|
|
} |
364
|
|
|
|
|
|
|
elsif ($base == 1000) |
365
|
|
|
|
|
|
|
{ |
366
|
2
|
|
|
|
|
8
|
return ( kilo => 1_000, |
367
|
|
|
|
|
|
|
mega => 1_000_000, |
368
|
|
|
|
|
|
|
giga => 1_000_000_000 ); |
369
|
|
|
|
|
|
|
} |
370
|
|
|
|
|
|
|
else |
371
|
|
|
|
|
|
|
{ |
372
|
5
|
100
|
|
|
|
305
|
croak "base overflow" if $base **3 > MAX_INT; |
373
|
4
|
100
|
100
|
|
|
555
|
croak "base must be a positive integer" |
374
|
|
|
|
|
|
|
unless $base > 0 && $base == int($base); |
375
|
1
|
|
|
|
|
8
|
return ( kilo => $base, |
376
|
|
|
|
|
|
|
mega => $base ** 2, |
377
|
|
|
|
|
|
|
giga => $base ** 3 ); |
378
|
|
|
|
|
|
|
} |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
###--------------------------------------------------------------------- |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
=head1 METHODS |
384
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
=over 4 |
386
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
=cut |
388
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
=item new( %args ) |
392
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
Creates a new Number::Format object. Valid keys for %args are any of |
394
|
|
|
|
|
|
|
the parameters described above. Keys may be in all uppercase or all |
395
|
|
|
|
|
|
|
lowercase, and may optionally be preceded by a hyphen (-) character. |
396
|
|
|
|
|
|
|
Example: |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
my $de = new Number::Format(-thousands_sep => '.', |
399
|
|
|
|
|
|
|
-decimal_point => ',', |
400
|
|
|
|
|
|
|
-int_curr_symbol => 'DEM'); |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
=cut |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
sub new |
405
|
|
|
|
|
|
|
{ |
406
|
11
|
|
|
11
|
1
|
4324
|
my $type = shift; |
407
|
11
|
|
|
|
|
63
|
my %args = @_; |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
# Fetch defaults from current locale, or failing that, using globals |
410
|
11
|
|
|
|
|
4482
|
my $me = {}; |
411
|
|
|
|
|
|
|
# my $locale = setlocale(LC_ALL, ""); |
412
|
11
|
|
|
|
|
256
|
my $locale_values = localeconv(); |
413
|
|
|
|
|
|
|
|
414
|
11
|
|
|
|
|
23
|
my $arg; |
415
|
|
|
|
|
|
|
|
416
|
11
|
|
|
|
|
103
|
while(my($arg, $default) = each %$DEFAULT_LOCALE) |
417
|
|
|
|
|
|
|
{ |
418
|
297
|
100
|
|
|
|
845
|
$me->{$arg} = (exists $locale_values->{$arg} |
419
|
|
|
|
|
|
|
? $locale_values->{$arg} |
420
|
|
|
|
|
|
|
: $default); |
421
|
|
|
|
|
|
|
|
422
|
297
|
|
|
|
|
647
|
foreach ($arg, uc $arg, "-$arg", uc "-$arg") |
423
|
|
|
|
|
|
|
{ |
424
|
1150
|
100
|
|
|
|
3354
|
next unless defined $args{$_}; |
425
|
38
|
|
|
|
|
54
|
$me->{$arg} = $args{$_}; |
426
|
38
|
|
|
|
|
51
|
delete $args{$_}; |
427
|
38
|
|
|
|
|
122
|
last; |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
} |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
# |
432
|
|
|
|
|
|
|
# Some broken locales define the decimal_point but not the |
433
|
|
|
|
|
|
|
# thousands_sep. If decimal_point is set to "," the default |
434
|
|
|
|
|
|
|
# thousands_sep will be a conflict. In that case, set |
435
|
|
|
|
|
|
|
# thousands_sep to empty string. Suggested by Moritz Onken. |
436
|
|
|
|
|
|
|
# |
437
|
11
|
|
|
|
|
29
|
foreach my $prefix ("", "mon_") |
438
|
|
|
|
|
|
|
{ |
439
|
22
|
50
|
|
|
|
149
|
$me->{"${prefix}thousands_sep"} = "" |
440
|
|
|
|
|
|
|
if ($me->{"${prefix}decimal_point"} eq |
441
|
|
|
|
|
|
|
$me->{"${prefix}thousands_sep"}); |
442
|
|
|
|
|
|
|
} |
443
|
|
|
|
|
|
|
|
444
|
11
|
50
|
|
|
|
46
|
croak "Invalid argument(s)" if %args; |
445
|
11
|
|
|
|
|
37
|
bless $me, $type; |
446
|
11
|
|
|
|
|
202
|
$me; |
447
|
|
|
|
|
|
|
} |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
=item round($number, $precision) |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
Rounds the number to the specified precision. If C<$precision> is |
454
|
|
|
|
|
|
|
omitted, the value of the C parameter is used (default |
455
|
|
|
|
|
|
|
value 2). Both input and output are numeric (the function uses math |
456
|
|
|
|
|
|
|
operators rather than string manipulation to do its job), The value of |
457
|
|
|
|
|
|
|
C<$precision> may be any integer, positive or negative. Examples: |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
round(3.14159) yields 3.14 |
460
|
|
|
|
|
|
|
round(3.14159, 4) yields 3.1416 |
461
|
|
|
|
|
|
|
round(42.00, 4) yields 42 |
462
|
|
|
|
|
|
|
round(1234, -2) yields 1200 |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
Since this is a mathematical rather than string oriented function, |
465
|
|
|
|
|
|
|
there will be no trailing zeroes to the right of the decimal point, |
466
|
|
|
|
|
|
|
and the C and C variables are ignored. |
467
|
|
|
|
|
|
|
To format your number using the C and C |
468
|
|
|
|
|
|
|
variables, use C instead. |
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
=cut |
471
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
sub round |
473
|
|
|
|
|
|
|
{ |
474
|
114
|
|
|
114
|
1
|
7214
|
my ($self, $number, $precision) = _get_self @_; |
475
|
114
|
100
|
|
|
|
316
|
$precision = $self->{decimal_digits} unless defined $precision; |
476
|
114
|
50
|
|
|
|
520
|
$precision = 2 unless defined $precision; |
477
|
114
|
50
|
|
|
|
212
|
$number = 0 unless defined $number; |
478
|
|
|
|
|
|
|
|
479
|
114
|
|
|
|
|
181
|
my $sign = $number <=> 0; |
480
|
114
|
|
|
|
|
228
|
my $multiplier = (10 ** $precision); |
481
|
114
|
|
|
|
|
141
|
my $result = abs($number); |
482
|
114
|
|
|
|
|
171
|
my $product = $result * $multiplier; |
483
|
|
|
|
|
|
|
|
484
|
114
|
100
|
|
|
|
469
|
croak "round() overflow. Try smaller precision or use Math::BigFloat" |
485
|
|
|
|
|
|
|
if $product > MAX_INT; |
486
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
# We need to add 1e-14 to avoid some rounding errors due to the |
488
|
|
|
|
|
|
|
# way floating point numbers work - see string-eq test in t/round.t |
489
|
113
|
|
|
|
|
208
|
$result = int($product + .5 + 1e-14) / $multiplier; |
490
|
113
|
100
|
|
|
|
228
|
$result = -$result if $sign < 0; |
491
|
113
|
|
|
|
|
249
|
return $result; |
492
|
|
|
|
|
|
|
} |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
=item format_number($number, $precision, $trailing_zeroes) |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
Formats a number by adding C between each set of 3 |
499
|
|
|
|
|
|
|
digits to the left of the decimal point, substituting C |
500
|
|
|
|
|
|
|
for the decimal point, and rounding to the specified precision using |
501
|
|
|
|
|
|
|
C. Note that C<$precision> is a I precision |
502
|
|
|
|
|
|
|
specifier; trailing zeroes will only appear in the output if |
503
|
|
|
|
|
|
|
C<$trailing_zeroes> is provided, or the parameter C is |
504
|
|
|
|
|
|
|
set, with a value that is true (not zero, undef, or the empty string). |
505
|
|
|
|
|
|
|
If C<$precision> is omitted, the value of the C |
506
|
|
|
|
|
|
|
parameter (default value of 2) is used. |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
If the value is too large or great to work with as a regular number, |
509
|
|
|
|
|
|
|
but instead must be shown in scientific notation, returns that number |
510
|
|
|
|
|
|
|
in scientific notation without further formatting. |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
Examples: |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
format_number(12345.6789) yields '12,345.68' |
515
|
|
|
|
|
|
|
format_number(123456.789, 2) yields '123,456.79' |
516
|
|
|
|
|
|
|
format_number(1234567.89, 2) yields '1,234,567.89' |
517
|
|
|
|
|
|
|
format_number(1234567.8, 2) yields '1,234,567.8' |
518
|
|
|
|
|
|
|
format_number(1234567.8, 2, 1) yields '1,234,567.80' |
519
|
|
|
|
|
|
|
format_number(1.23456789, 6) yields '1.234568' |
520
|
|
|
|
|
|
|
format_number("0.000020000E+00", 7);' yields '2e-05' |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
Of course the output would have your values of C and |
523
|
|
|
|
|
|
|
C instead of ',' and '.' respectively. |
524
|
|
|
|
|
|
|
|
525
|
|
|
|
|
|
|
=cut |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
sub format_number |
528
|
|
|
|
|
|
|
{ |
529
|
87
|
|
|
87
|
1
|
1341
|
my ($self, $number, $precision, $trailing_zeroes, $mon) = _get_self @_; |
530
|
87
|
|
|
|
|
210
|
$self->_check_seps(); # first make sure the SEP variables are valid |
531
|
|
|
|
|
|
|
|
532
|
87
|
100
|
|
|
|
293
|
my($thousands_sep, $decimal_point) = |
533
|
|
|
|
|
|
|
$mon ? @$self{qw(mon_thousands_sep mon_decimal_point)} |
534
|
|
|
|
|
|
|
: @$self{qw(thousands_sep decimal_point)}; |
535
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
# Set defaults and standardize number |
537
|
87
|
100
|
|
|
|
226
|
$precision = $self->{decimal_digits} unless defined $precision; |
538
|
87
|
100
|
|
|
|
286
|
$trailing_zeroes = $self->{decimal_fill} unless defined $trailing_zeroes; |
539
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
# Handle negative numbers |
541
|
87
|
|
|
|
|
126
|
my $sign = $number <=> 0; |
542
|
87
|
100
|
|
|
|
158
|
$number = abs($number) if $sign < 0; |
543
|
87
|
|
|
|
|
258
|
$number = $self->round($number, $precision); # round off $number |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
# detect scientific notation |
546
|
86
|
|
|
|
|
114
|
my $exponent = 0; |
547
|
86
|
50
|
|
|
|
657
|
if ($number =~ /^(-?[\d.]+)e([+-]\d+)$/) |
548
|
|
|
|
|
|
|
{ |
549
|
|
|
|
|
|
|
# Don't attempt to format numbers that require scientific notation. |
550
|
0
|
|
|
|
|
0
|
return $number; |
551
|
|
|
|
|
|
|
} |
552
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
# Split integer and decimal parts of the number and add commas |
554
|
86
|
|
|
|
|
123
|
my $integer = int($number); |
555
|
86
|
|
|
|
|
86
|
my $decimal; |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
# Note: In perl 5.6 and up, string representation of a number |
558
|
|
|
|
|
|
|
# automagically includes the locale decimal point. This way we |
559
|
|
|
|
|
|
|
# will detect the decimal part correctly as long as the decimal |
560
|
|
|
|
|
|
|
# point is 1 character. |
561
|
86
|
100
|
|
|
|
523
|
$decimal = substr($number, length($integer)+1) |
562
|
|
|
|
|
|
|
if (length($integer) < length($number)); |
563
|
86
|
100
|
|
|
|
153
|
$decimal = '' unless defined $decimal; |
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
# Add trailing 0's if $trailing_zeroes is set. |
566
|
86
|
100
|
100
|
|
|
587
|
$decimal .= '0'x( $precision - length($decimal) ) |
567
|
|
|
|
|
|
|
if $trailing_zeroes && $precision > length($decimal); |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
# Add the commas (or whatever is in thousands_sep). If |
570
|
|
|
|
|
|
|
# thousands_sep is the empty string, do nothing. |
571
|
86
|
50
|
|
|
|
197
|
if ($thousands_sep) |
572
|
|
|
|
|
|
|
{ |
573
|
|
|
|
|
|
|
# Add leading 0's so length($integer) is divisible by 3 |
574
|
86
|
|
|
|
|
214
|
$integer = '0'x(3 - (length($integer) % 3)).$integer; |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
# Split $integer into groups of 3 characters and insert commas |
577
|
238
|
|
|
|
|
1610
|
$integer = join($thousands_sep, |
578
|
86
|
|
|
|
|
412
|
grep {$_ ne ''} split(/(...)/, $integer)); |
579
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
# Strip off leading zeroes and/or comma |
581
|
86
|
|
|
|
|
639
|
$integer =~ s/^0+\Q$thousands_sep\E?//; |
582
|
|
|
|
|
|
|
} |
583
|
86
|
100
|
|
|
|
195
|
$integer = '0' if $integer eq ''; |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
# Combine integer and decimal parts and return the result. |
586
|
86
|
100
|
66
|
|
|
487
|
my $result = ((defined $decimal && length $decimal) ? |
587
|
|
|
|
|
|
|
join($decimal_point, $integer, $decimal) : |
588
|
|
|
|
|
|
|
$integer); |
589
|
|
|
|
|
|
|
|
590
|
86
|
100
|
|
|
|
468
|
return ($sign < 0) ? $self->format_negative($result) : $result; |
591
|
|
|
|
|
|
|
} |
592
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
594
|
|
|
|
|
|
|
|
595
|
|
|
|
|
|
|
=item format_negative($number, $picture) |
596
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
Formats a negative number. Picture should be a string that contains |
598
|
|
|
|
|
|
|
the letter C where the number should be inserted. For example, for |
599
|
|
|
|
|
|
|
standard negative numbers you might use ``C<-x>'', while for |
600
|
|
|
|
|
|
|
accounting purposes you might use ``C<(x)>''. If the specified number |
601
|
|
|
|
|
|
|
begins with a ``-'' character, that will be removed before formatting, |
602
|
|
|
|
|
|
|
but formatting will occur whether or not the number is negative. |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
=cut |
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
sub format_negative |
607
|
|
|
|
|
|
|
{ |
608
|
7
|
|
|
7
|
1
|
36
|
my($self, $number, $format) = _get_self @_; |
609
|
7
|
50
|
|
|
|
28
|
$format = $self->{neg_format} unless defined $format; |
610
|
7
|
50
|
|
|
|
27
|
croak "Letter x must be present in picture in format_negative()" |
611
|
|
|
|
|
|
|
unless $format =~ /x/; |
612
|
7
|
|
|
|
|
29
|
$number =~ s/^-//; |
613
|
7
|
|
|
|
|
24
|
$format =~ s/x/$number/; |
614
|
7
|
|
|
|
|
48
|
return $format; |
615
|
|
|
|
|
|
|
} |
616
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
618
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
=item format_picture($number, $picture) |
620
|
|
|
|
|
|
|
|
621
|
|
|
|
|
|
|
Returns a string based on C<$picture> with the C<#> characters |
622
|
|
|
|
|
|
|
replaced by digits from C<$number>. If the length of the integer part |
623
|
|
|
|
|
|
|
of $number is too large to fit, the C<#> characters are replaced with |
624
|
|
|
|
|
|
|
asterisks (C<*>) instead. Examples: |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
format_picture(100.023, 'USD ##,###.##') yields 'USD 100.02' |
627
|
|
|
|
|
|
|
format_picture(1000.23, 'USD ##,###.##') yields 'USD 1,000.23' |
628
|
|
|
|
|
|
|
format_picture(10002.3, 'USD ##,###.##') yields 'USD 10,002.30' |
629
|
|
|
|
|
|
|
format_picture(100023, 'USD ##,###.##') yields 'USD **,***.**' |
630
|
|
|
|
|
|
|
format_picture(1.00023, 'USD #.###,###') yields 'USD 1.002,300' |
631
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
The comma (,) and period (.) you see in the picture examples should |
633
|
|
|
|
|
|
|
match the values of C and C, |
634
|
|
|
|
|
|
|
respectively, for proper operation. However, the C |
635
|
|
|
|
|
|
|
characters in C<$picture> need not occur every three digits; the |
636
|
|
|
|
|
|
|
I use of that variable by this function is to remove leading |
637
|
|
|
|
|
|
|
commas (see the first example above). There may not be more than one |
638
|
|
|
|
|
|
|
instance of C in C<$picture>. |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
The value of C is used to determine how negative numbers |
641
|
|
|
|
|
|
|
are displayed. The result of this is that the output of this function |
642
|
|
|
|
|
|
|
my have unexpected spaces before and/or after the number. This is |
643
|
|
|
|
|
|
|
necessary so that positive and negative numbers are formatted into a |
644
|
|
|
|
|
|
|
space the same size. If you are only using positive numbers and want |
645
|
|
|
|
|
|
|
to avoid this problem, set NEG_FORMAT to "x". |
646
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
=cut |
648
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
sub format_picture |
650
|
|
|
|
|
|
|
{ |
651
|
12
|
|
|
12
|
1
|
50
|
my ($self, $number, $picture) = _get_self @_; |
652
|
12
|
|
|
|
|
33
|
$self->_check_seps(); |
653
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
# Handle negative numbers |
655
|
12
|
|
|
|
|
61
|
my($neg_prefix) = $self->{neg_format} =~ /^([^x]+)/; |
656
|
12
|
|
|
|
|
33
|
my($pic_prefix) = $picture =~ /^([^\#]+)/; |
657
|
12
|
|
|
|
|
33
|
my $neg_pic = $self->{neg_format}; |
658
|
12
|
|
|
|
|
59
|
(my $pos_pic = $self->{neg_format}) =~ s/[^x\s]/ /g; |
659
|
12
|
|
|
|
|
55
|
(my $pos_prefix = $neg_prefix) =~ s/[^x\s]/ /g; |
660
|
12
|
|
|
|
|
39
|
$neg_pic =~ s/x/$picture/; |
661
|
12
|
|
|
|
|
31
|
$pos_pic =~ s/x/$picture/; |
662
|
12
|
|
|
|
|
25
|
my $sign = $number <=> 0; |
663
|
12
|
100
|
|
|
|
36
|
$number = abs($number) if $sign < 0; |
664
|
12
|
100
|
|
|
|
27
|
$picture = $sign < 0 ? $neg_pic : $pos_pic; |
665
|
12
|
100
|
|
|
|
26
|
my $sign_prefix = $sign < 0 ? $neg_prefix : $pos_prefix; |
666
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
# Split up the picture and die if there is more than one $DECIMAL_POINT |
668
|
12
|
|
|
|
|
81
|
my($pic_int, $pic_dec, @cruft) = |
669
|
|
|
|
|
|
|
split(/\Q$self->{decimal_point}\E/, $picture); |
670
|
12
|
50
|
|
|
|
29
|
$pic_int = '' unless defined $pic_int; |
671
|
12
|
100
|
|
|
|
41
|
$pic_dec = '' unless defined $pic_dec; |
672
|
|
|
|
|
|
|
|
673
|
12
|
50
|
|
|
|
32
|
croak "Only one decimal separator permitted in picture" |
674
|
|
|
|
|
|
|
if @cruft; |
675
|
|
|
|
|
|
|
|
676
|
|
|
|
|
|
|
# Obtain precision from the length of the decimal part... |
677
|
12
|
|
|
|
|
18
|
my $precision = $pic_dec; # start with copying it |
678
|
12
|
|
|
|
|
20
|
$precision =~ s/[^\#]//g; # eliminate all non-# characters |
679
|
12
|
|
|
|
|
19
|
$precision = length $precision; # take the length of the result |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
# Format the number |
682
|
12
|
|
|
|
|
31
|
$number = $self->round($number, $precision); |
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
# Obtain the length of the integer portion just like we did for $precision |
685
|
12
|
|
|
|
|
22
|
my $intsize = $pic_int; # start with copying it |
686
|
12
|
|
|
|
|
71
|
$intsize =~ s/[^\#]//g; # eliminate all non-# characters |
687
|
12
|
|
|
|
|
16
|
$intsize = length $intsize; # take the length of the result |
688
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
# Split up $number same as we did for $picture earlier |
690
|
12
|
|
|
|
|
108
|
my($num_int, $num_dec) = split(/\./, $number, 2); |
691
|
12
|
50
|
|
|
|
30
|
$num_int = '' unless defined $num_int; |
692
|
12
|
100
|
|
|
|
23
|
$num_dec = '' unless defined $num_dec; |
693
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
# Check if the integer part will fit in the picture |
695
|
12
|
100
|
|
|
|
26
|
if (length $num_int > $intsize) |
696
|
|
|
|
|
|
|
{ |
697
|
2
|
|
|
|
|
12
|
$picture =~ s/\#/\*/g; # convert # to * and return it |
698
|
2
|
50
|
|
|
|
7
|
$pic_prefix = "" unless defined $pic_prefix; |
699
|
2
|
|
|
|
|
57
|
$picture =~ s/^(\Q$sign_prefix\E)(\Q$pic_prefix\E)(\s*)/$2$3$1/; |
700
|
2
|
|
|
|
|
25
|
return $picture; |
701
|
|
|
|
|
|
|
} |
702
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
# Split each portion of number and picture into arrays of characters |
704
|
10
|
|
|
|
|
38
|
my @num_int = split(//, $num_int); |
705
|
10
|
|
|
|
|
23
|
my @num_dec = split(//, $num_dec); |
706
|
10
|
|
|
|
|
54
|
my @pic_int = split(//, $pic_int); |
707
|
10
|
|
|
|
|
31
|
my @pic_dec = split(//, $pic_dec); |
708
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
# Now we copy those characters into @result. |
710
|
10
|
|
|
|
|
59
|
my @result; |
711
|
10
|
100
|
|
|
|
59
|
@result = ($self->{decimal_point}) |
712
|
|
|
|
|
|
|
if $picture =~ /\Q$self->{decimal_point}\E/; |
713
|
|
|
|
|
|
|
# For each characture in the decimal part of the picture, replace '#' |
714
|
|
|
|
|
|
|
# signs with digits from the number. |
715
|
10
|
|
|
|
|
14
|
my $char; |
716
|
10
|
|
|
|
|
17
|
foreach $char (@pic_dec) |
717
|
|
|
|
|
|
|
{ |
718
|
18
|
100
|
100
|
|
|
58
|
$char = (shift(@num_dec) || 0) if ($char eq '#'); |
719
|
18
|
|
|
|
|
52
|
push (@result, $char); |
720
|
|
|
|
|
|
|
} |
721
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
# For each character in the integer part of the picture (moving right |
723
|
|
|
|
|
|
|
# to left this time), replace '#' signs with digits from the number, |
724
|
|
|
|
|
|
|
# or spaces if we've run out of numbers. |
725
|
10
|
|
|
|
|
28
|
while ($char = pop @pic_int) |
726
|
|
|
|
|
|
|
{ |
727
|
118
|
100
|
|
|
|
239
|
$char = pop(@num_int) if ($char eq '#'); |
728
|
118
|
100
|
100
|
|
|
420
|
$char = ' ' if (!defined($char) || |
|
|
|
66
|
|
|
|
|
729
|
|
|
|
|
|
|
$char eq $self->{thousands_sep} && $#num_int < 0); |
730
|
118
|
|
|
|
|
324
|
unshift (@result, $char); |
731
|
|
|
|
|
|
|
} |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
# Combine @result into a string and return it. |
734
|
10
|
|
|
|
|
28
|
my $result = join('', @result); |
735
|
10
|
50
|
|
|
|
19
|
$sign_prefix = '' unless defined $sign_prefix; |
736
|
10
|
100
|
|
|
|
20
|
$pic_prefix = '' unless defined $pic_prefix; |
737
|
10
|
|
|
|
|
204
|
$result =~ s/^(\Q$sign_prefix\E)(\Q$pic_prefix\E)(\s*)/$2$3$1/; |
738
|
10
|
|
|
|
|
90
|
$result; |
739
|
|
|
|
|
|
|
} |
740
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
742
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
=item format_price($number, $precision, $symbol) |
744
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
Returns a string containing C<$number> formatted similarly to |
746
|
|
|
|
|
|
|
C, except that the decimal portion may have trailing |
747
|
|
|
|
|
|
|
zeroes added to make it be exactly C<$precision> characters long, and |
748
|
|
|
|
|
|
|
the currency string will be prefixed. |
749
|
|
|
|
|
|
|
|
750
|
|
|
|
|
|
|
The C<$symbol> attribute may be one of "INT_CURR_SYMBOL" or |
751
|
|
|
|
|
|
|
"CURRENCY_SYMBOL" (case insensitive) to use the value of that |
752
|
|
|
|
|
|
|
attribute of the object, or a string containing the symbol to be used. |
753
|
|
|
|
|
|
|
The default is "INT_CURR_SYMBOL" if this argument is undefined or not |
754
|
|
|
|
|
|
|
given; if set to the empty string, or if set to undef and the |
755
|
|
|
|
|
|
|
C attribute of the object is the empty string, no |
756
|
|
|
|
|
|
|
currency will be added. |
757
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
If C<$precision> is not provided, the default of 2 will be used. |
759
|
|
|
|
|
|
|
Examples: |
760
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
format_price(12.95) yields 'USD 12.95' |
762
|
|
|
|
|
|
|
format_price(12) yields 'USD 12.00' |
763
|
|
|
|
|
|
|
format_price(12, 3) yields '12.000' |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
The third example assumes that C is the empty string. |
766
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
=cut |
768
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
sub format_price |
770
|
|
|
|
|
|
|
{ |
771
|
54
|
|
|
54
|
1
|
17708
|
my ($self, $number, $precision, $curr_symbol) = _get_self @_; |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
# Determine what the monetary symbol should be |
774
|
54
|
100
|
66
|
|
|
193
|
$curr_symbol = $self->{int_curr_symbol} |
775
|
|
|
|
|
|
|
if (!defined($curr_symbol) || lc($curr_symbol) eq "int_curr_symbol"); |
776
|
54
|
100
|
66
|
|
|
266
|
$curr_symbol = $self->{currency_symbol} |
777
|
|
|
|
|
|
|
if (!defined($curr_symbol) || lc($curr_symbol) eq "currency_symbol"); |
778
|
54
|
50
|
|
|
|
107
|
$curr_symbol = "" unless defined($curr_symbol); |
779
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
# Determine which value to use for frac digits |
781
|
54
|
100
|
|
|
|
126
|
my $frac_digits = ($curr_symbol eq $self->{int_curr_symbol} ? |
782
|
|
|
|
|
|
|
$self->{int_frac_digits} : $self->{frac_digits}); |
783
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
# Determine precision for decimal portion |
785
|
54
|
100
|
|
|
|
152
|
$precision = $frac_digits unless defined $precision; |
786
|
54
|
50
|
|
|
|
96
|
$precision = $self->{decimal_digits} unless defined $precision; # fallback |
787
|
54
|
50
|
|
|
|
82
|
$precision = 2 unless defined $precision; # default |
788
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
# Determine sign and absolute value |
790
|
54
|
|
|
|
|
84
|
my $sign = $number <=> 0; |
791
|
54
|
100
|
|
|
|
97
|
$number = abs($number) if $sign < 0; |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
# format it first |
794
|
54
|
|
|
|
|
134
|
$number = $self->format_number($number, $precision, undef, 1); |
795
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
# Now we make sure the decimal part has enough zeroes |
797
|
54
|
|
|
|
|
263
|
my ($integer, $decimal) = |
798
|
|
|
|
|
|
|
split(/\Q$self->{mon_decimal_point}\E/, $number, 2); |
799
|
54
|
100
|
|
|
|
107
|
$decimal = '0'x$precision unless $decimal; |
800
|
54
|
|
|
|
|
110
|
$decimal .= '0'x($precision - length $decimal); |
801
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
# Extract positive or negative values |
803
|
54
|
|
|
|
|
62
|
my($sep_by_space, $cs_precedes, $sign_posn, $sign_symbol); |
804
|
54
|
100
|
|
|
|
80
|
if ($sign < 0) |
805
|
|
|
|
|
|
|
{ |
806
|
41
|
|
|
|
|
62
|
$sep_by_space = $self->{n_sep_by_space}; |
807
|
41
|
|
|
|
|
51
|
$cs_precedes = $self->{n_cs_precedes}; |
808
|
41
|
|
|
|
|
52
|
$sign_posn = $self->{n_sign_posn}; |
809
|
41
|
|
|
|
|
57
|
$sign_symbol = $self->{negative_sign}; |
810
|
|
|
|
|
|
|
} |
811
|
|
|
|
|
|
|
else |
812
|
|
|
|
|
|
|
{ |
813
|
13
|
|
|
|
|
17
|
$sep_by_space = $self->{p_sep_by_space}; |
814
|
13
|
|
|
|
|
16
|
$cs_precedes = $self->{p_cs_precedes}; |
815
|
13
|
|
|
|
|
20
|
$sign_posn = $self->{p_sign_posn}; |
816
|
13
|
|
|
|
|
15
|
$sign_symbol = $self->{positive_sign}; |
817
|
|
|
|
|
|
|
} |
818
|
|
|
|
|
|
|
|
819
|
|
|
|
|
|
|
# Combine it all back together. |
820
|
54
|
100
|
|
|
|
216
|
my $result = ($precision ? |
821
|
|
|
|
|
|
|
join($self->{mon_decimal_point}, $integer, $decimal) : |
822
|
|
|
|
|
|
|
$integer); |
823
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
# Determine where spaces go, if any |
825
|
54
|
|
|
|
|
59
|
my($sign_sep, $curr_sep); |
826
|
54
|
100
|
|
|
|
141
|
if ($sep_by_space == 0) |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
827
|
|
|
|
|
|
|
{ |
828
|
20
|
|
|
|
|
27
|
$sign_sep = $curr_sep = ""; |
829
|
|
|
|
|
|
|
} |
830
|
|
|
|
|
|
|
elsif ($sep_by_space == 1) |
831
|
|
|
|
|
|
|
{ |
832
|
22
|
|
|
|
|
25
|
$sign_sep = ""; |
833
|
22
|
|
|
|
|
29
|
$curr_sep = " "; |
834
|
|
|
|
|
|
|
} |
835
|
|
|
|
|
|
|
elsif ($sep_by_space == 2) |
836
|
|
|
|
|
|
|
{ |
837
|
12
|
|
|
|
|
17
|
$sign_sep = " "; |
838
|
12
|
|
|
|
|
15
|
$curr_sep = ""; |
839
|
|
|
|
|
|
|
} |
840
|
|
|
|
|
|
|
else |
841
|
|
|
|
|
|
|
{ |
842
|
0
|
|
|
|
|
0
|
croak "Invalid sep_by_space value"; |
843
|
|
|
|
|
|
|
} |
844
|
|
|
|
|
|
|
|
845
|
|
|
|
|
|
|
# Add sign, if any |
846
|
54
|
100
|
66
|
|
|
232
|
if ($sign_posn >= 0 && $sign_posn <= 2) |
|
|
50
|
66
|
|
|
|
|
847
|
|
|
|
|
|
|
{ |
848
|
|
|
|
|
|
|
# Combine with currency symbol and return |
849
|
34
|
50
|
|
|
|
64
|
if ($curr_symbol ne "") |
850
|
|
|
|
|
|
|
{ |
851
|
34
|
100
|
|
|
|
44
|
if ($cs_precedes) |
852
|
|
|
|
|
|
|
{ |
853
|
25
|
|
|
|
|
41
|
$result = $curr_symbol.$curr_sep.$result; |
854
|
|
|
|
|
|
|
} |
855
|
|
|
|
|
|
|
else |
856
|
|
|
|
|
|
|
{ |
857
|
9
|
|
|
|
|
14
|
$result = $result.$curr_sep.$curr_symbol; |
858
|
|
|
|
|
|
|
} |
859
|
|
|
|
|
|
|
} |
860
|
|
|
|
|
|
|
|
861
|
34
|
100
|
|
|
|
64
|
if ($sign_posn == 0) |
|
|
100
|
|
|
|
|
|
862
|
|
|
|
|
|
|
{ |
863
|
7
|
|
|
|
|
41
|
return "($result)"; |
864
|
|
|
|
|
|
|
} |
865
|
|
|
|
|
|
|
elsif ($sign_posn == 1) |
866
|
|
|
|
|
|
|
{ |
867
|
20
|
|
|
|
|
102
|
return $sign_symbol.$sign_sep.$result; |
868
|
|
|
|
|
|
|
} |
869
|
|
|
|
|
|
|
else # $sign_posn == 2 |
870
|
|
|
|
|
|
|
{ |
871
|
7
|
|
|
|
|
37
|
return $result.$sign_sep.$sign_symbol; |
872
|
|
|
|
|
|
|
} |
873
|
|
|
|
|
|
|
} |
874
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
elsif ($sign_posn == 3 || $sign_posn == 4) |
876
|
|
|
|
|
|
|
{ |
877
|
20
|
100
|
|
|
|
67
|
if ($sign_posn == 3) |
878
|
|
|
|
|
|
|
{ |
879
|
13
|
|
|
|
|
47
|
$curr_symbol = $sign_symbol.$sign_sep.$curr_symbol; |
880
|
|
|
|
|
|
|
} |
881
|
|
|
|
|
|
|
else # $sign_posn == 4 |
882
|
|
|
|
|
|
|
{ |
883
|
7
|
|
|
|
|
12
|
$curr_symbol = $curr_symbol.$sign_sep.$sign_symbol; |
884
|
|
|
|
|
|
|
} |
885
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
# Combine with currency symbol and return |
887
|
20
|
100
|
|
|
|
33
|
if ($cs_precedes) |
888
|
|
|
|
|
|
|
{ |
889
|
11
|
|
|
|
|
58
|
return $curr_symbol.$curr_sep.$result; |
890
|
|
|
|
|
|
|
} |
891
|
|
|
|
|
|
|
else |
892
|
|
|
|
|
|
|
{ |
893
|
9
|
|
|
|
|
266
|
return $result.$curr_sep.$curr_symbol; |
894
|
|
|
|
|
|
|
} |
895
|
|
|
|
|
|
|
} |
896
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
else |
898
|
|
|
|
|
|
|
{ |
899
|
0
|
|
|
|
|
0
|
croak "Invalid *_sign_posn value"; |
900
|
|
|
|
|
|
|
} |
901
|
|
|
|
|
|
|
} |
902
|
|
|
|
|
|
|
|
903
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
904
|
|
|
|
|
|
|
|
905
|
|
|
|
|
|
|
=item format_bytes($number, %options) |
906
|
|
|
|
|
|
|
|
907
|
|
|
|
|
|
|
=item format_bytes($number, $precision) # deprecated |
908
|
|
|
|
|
|
|
|
909
|
|
|
|
|
|
|
Returns a string containing C<$number> formatted similarly to |
910
|
|
|
|
|
|
|
C, except that large numbers may be abbreviated by |
911
|
|
|
|
|
|
|
adding a suffix to indicate 1024, 1,048,576, or 1,073,741,824 bytes. |
912
|
|
|
|
|
|
|
Suffix may be the traditional K, M, or G (default); or the IEC |
913
|
|
|
|
|
|
|
standard 60027 "KiB," "MiB," or "GiB" depending on the "mode" option. |
914
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
Negative values will result in an error. |
916
|
|
|
|
|
|
|
|
917
|
|
|
|
|
|
|
The second parameter can be either a hash that sets options, or a |
918
|
|
|
|
|
|
|
number. Using a number here is deprecated and will generate a |
919
|
|
|
|
|
|
|
warning; early versions of Number::Format only allowed a numeric |
920
|
|
|
|
|
|
|
value. A future release of Number::Format will change this warning to |
921
|
|
|
|
|
|
|
an error. New code should use a hash instead to set options. If it |
922
|
|
|
|
|
|
|
is a number this sets the value of the "precision" option. |
923
|
|
|
|
|
|
|
|
924
|
|
|
|
|
|
|
Valid options are: |
925
|
|
|
|
|
|
|
|
926
|
|
|
|
|
|
|
=over 4 |
927
|
|
|
|
|
|
|
|
928
|
|
|
|
|
|
|
=item precision |
929
|
|
|
|
|
|
|
|
930
|
|
|
|
|
|
|
Set the precision for displaying numbers. If not provided, a default |
931
|
|
|
|
|
|
|
of 2 will be used. Examples: |
932
|
|
|
|
|
|
|
|
933
|
|
|
|
|
|
|
format_bytes(12.95) yields '12.95' |
934
|
|
|
|
|
|
|
format_bytes(12.95, precision => 0) yields '13' |
935
|
|
|
|
|
|
|
format_bytes(2048) yields '2K' |
936
|
|
|
|
|
|
|
format_bytes(2048, mode => "iec") yields '2KiB' |
937
|
|
|
|
|
|
|
format_bytes(9999999) yields '9.54M' |
938
|
|
|
|
|
|
|
format_bytes(9999999, precision => 1) yields '9.5M' |
939
|
|
|
|
|
|
|
|
940
|
|
|
|
|
|
|
=item unit |
941
|
|
|
|
|
|
|
|
942
|
|
|
|
|
|
|
Sets the default units used for the results. The default is to |
943
|
|
|
|
|
|
|
determine this automatically in order to minimize the length of the |
944
|
|
|
|
|
|
|
string. In other words, numbers greater than or equal to 1024 (or |
945
|
|
|
|
|
|
|
other number given by the 'base' option, q.v.) will be divided by 1024 |
946
|
|
|
|
|
|
|
and C<$KILO_SUFFIX> or C<$KIBI_SUFFIX> added; if greater than or equal |
947
|
|
|
|
|
|
|
to 1048576 (1024*1024), it will be divided by 1048576 and |
948
|
|
|
|
|
|
|
C<$MEGA_SUFFIX> or C<$MEBI_SUFFIX> appended to the end; etc. |
949
|
|
|
|
|
|
|
|
950
|
|
|
|
|
|
|
However if a value is given for C it will use that value |
951
|
|
|
|
|
|
|
instead. The first letter (case-insensitive) of the value given |
952
|
|
|
|
|
|
|
indicates the threshhold for conversion; acceptable values are G (for |
953
|
|
|
|
|
|
|
giga/gibi), M (for mega/mebi), K (for kilo/kibi), or A (for automatic, |
954
|
|
|
|
|
|
|
the default). For example: |
955
|
|
|
|
|
|
|
|
956
|
|
|
|
|
|
|
format_bytes(1048576, unit => 'K') yields '1,024K' |
957
|
|
|
|
|
|
|
instead of '1M' |
958
|
|
|
|
|
|
|
|
959
|
|
|
|
|
|
|
Note that the valid values to this option do not vary even when the |
960
|
|
|
|
|
|
|
suffix configuration variables have been changed. |
961
|
|
|
|
|
|
|
|
962
|
|
|
|
|
|
|
=item base |
963
|
|
|
|
|
|
|
|
964
|
|
|
|
|
|
|
Sets the number at which the C<$KILO_SUFFIX> is added. Default is |
965
|
|
|
|
|
|
|
1024. Set to any value; the only other useful value is probably 1000, |
966
|
|
|
|
|
|
|
as hard disk manufacturers use that number to make their disks sound |
967
|
|
|
|
|
|
|
bigger than they really are. |
968
|
|
|
|
|
|
|
|
969
|
|
|
|
|
|
|
If the mode (see below) is set to "iec" or "iec60027" then setting the |
970
|
|
|
|
|
|
|
base option results in an error. |
971
|
|
|
|
|
|
|
|
972
|
|
|
|
|
|
|
=item mode |
973
|
|
|
|
|
|
|
|
974
|
|
|
|
|
|
|
Traditionally, bytes have been given in SI (metric) units such as |
975
|
|
|
|
|
|
|
"kilo" and "mega" even though they represent powers of 2 (1024, etc.) |
976
|
|
|
|
|
|
|
rather than powers of 10 (1000, etc.) This "binary prefix" causes |
977
|
|
|
|
|
|
|
much confusion in consumer products where "GB" may mean either |
978
|
|
|
|
|
|
|
1,048,576 or 1,000,000, for example. The International |
979
|
|
|
|
|
|
|
Electrotechnical Commission has created standard IEC 60027 to |
980
|
|
|
|
|
|
|
introduce prefixes Ki, Mi, Gi, etc. ("kibibytes," "mebibytes," |
981
|
|
|
|
|
|
|
"gibibytes," etc.) to remove this confusion. Specify a mode option |
982
|
|
|
|
|
|
|
with either "traditional" or "iec60027" (or abbreviate as "trad" or |
983
|
|
|
|
|
|
|
"iec") to indicate which type of binary prefix you want format_bytes |
984
|
|
|
|
|
|
|
to use. For backward compatibility, "traditional" is the default. |
985
|
|
|
|
|
|
|
See http://en.wikipedia.org/wiki/Binary_prefix for more information. |
986
|
|
|
|
|
|
|
|
987
|
|
|
|
|
|
|
=back |
988
|
|
|
|
|
|
|
|
989
|
|
|
|
|
|
|
=cut |
990
|
|
|
|
|
|
|
|
991
|
|
|
|
|
|
|
sub format_bytes |
992
|
|
|
|
|
|
|
{ |
993
|
17
|
|
|
17
|
1
|
56
|
my ($self, $number, @options) = _get_self @_; |
994
|
|
|
|
|
|
|
|
995
|
17
|
50
|
|
|
|
47
|
croak "Negative number not allowed in format_bytes" |
996
|
|
|
|
|
|
|
if $number < 0; |
997
|
|
|
|
|
|
|
|
998
|
|
|
|
|
|
|
# If a single scalar is given instead of key/value pairs for |
999
|
|
|
|
|
|
|
# @options, treat that as the value of the precision option. |
1000
|
17
|
|
|
|
|
20
|
my %options; |
1001
|
17
|
50
|
|
|
|
33
|
if (@options == 1) |
1002
|
|
|
|
|
|
|
{ |
1003
|
|
|
|
|
|
|
# To be changed to 'croak' in a future release: |
1004
|
0
|
|
|
|
|
0
|
carp "format_bytes: number instead of options is deprecated"; |
1005
|
0
|
|
|
|
|
0
|
%options = ( precision => $options[0] ); |
1006
|
|
|
|
|
|
|
} |
1007
|
|
|
|
|
|
|
else |
1008
|
|
|
|
|
|
|
{ |
1009
|
17
|
|
|
|
|
35
|
%options = @options; |
1010
|
|
|
|
|
|
|
} |
1011
|
|
|
|
|
|
|
|
1012
|
|
|
|
|
|
|
# Set default for precision. Test using defined because it may be 0. |
1013
|
17
|
100
|
|
|
|
55
|
$options{precision} = $self->{decimal_digits} |
1014
|
|
|
|
|
|
|
unless defined $options{precision}; |
1015
|
17
|
50
|
|
|
|
40
|
$options{precision} = 2 |
1016
|
|
|
|
|
|
|
unless defined $options{precision}; # default |
1017
|
|
|
|
|
|
|
|
1018
|
17
|
|
100
|
|
|
62
|
$options{mode} ||= "traditional"; |
1019
|
17
|
|
|
|
|
16
|
my($ksuff, $msuff, $gsuff); |
1020
|
17
|
100
|
|
|
|
111
|
if ($options{mode} =~ /^iec(60027)?$/i) |
|
|
50
|
|
|
|
|
|
1021
|
|
|
|
|
|
|
{ |
1022
|
6
|
|
|
|
|
19
|
($ksuff, $msuff, $gsuff) = |
1023
|
|
|
|
|
|
|
@$self{qw(kibi_suffix mebi_suffix gibi_suffix)}; |
1024
|
6
|
50
|
|
|
|
17
|
croak "base option not allowed in iec60027 mode" |
1025
|
|
|
|
|
|
|
if exists $options{base}; |
1026
|
|
|
|
|
|
|
} |
1027
|
|
|
|
|
|
|
elsif ($options{mode} =~ /^trad(itional)?$/i) |
1028
|
|
|
|
|
|
|
{ |
1029
|
11
|
|
|
|
|
40
|
($ksuff, $msuff, $gsuff) = |
1030
|
|
|
|
|
|
|
@$self{qw(kilo_suffix mega_suffix giga_suffix)}; |
1031
|
|
|
|
|
|
|
} |
1032
|
|
|
|
|
|
|
else |
1033
|
|
|
|
|
|
|
{ |
1034
|
0
|
|
|
|
|
0
|
croak "Invalid mode"; |
1035
|
|
|
|
|
|
|
} |
1036
|
|
|
|
|
|
|
|
1037
|
|
|
|
|
|
|
# Set default for "base" option. Calculate threshold values for |
1038
|
|
|
|
|
|
|
# kilo, mega, and giga values. On 32-bit systems tera would cause |
1039
|
|
|
|
|
|
|
# overflows so it is not supported. Useful values of "base" are |
1040
|
|
|
|
|
|
|
# 1024 or 1000, but any number can be used. Larger numbers may |
1041
|
|
|
|
|
|
|
# cause overflows for giga or even mega, however. |
1042
|
17
|
|
|
|
|
62
|
my %mult = _get_multipliers($options{base}); |
1043
|
|
|
|
|
|
|
|
1044
|
|
|
|
|
|
|
# Process "unit" option. Set default, then take first character |
1045
|
|
|
|
|
|
|
# and convert to upper case. |
1046
|
17
|
100
|
|
|
|
69
|
$options{unit} = "auto" |
1047
|
|
|
|
|
|
|
unless defined $options{unit}; |
1048
|
17
|
|
|
|
|
41
|
my $unit = uc(substr($options{unit},0,1)); |
1049
|
|
|
|
|
|
|
|
1050
|
|
|
|
|
|
|
# Process "auto" first (default). Based on size of number, |
1051
|
|
|
|
|
|
|
# automatically determine which unit to use. |
1052
|
17
|
100
|
|
|
|
41
|
if ($unit eq 'A') |
1053
|
|
|
|
|
|
|
{ |
1054
|
15
|
100
|
|
|
|
69
|
if ($number >= $mult{giga}) |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
1055
|
|
|
|
|
|
|
{ |
1056
|
2
|
|
|
|
|
4
|
$unit = 'G'; |
1057
|
|
|
|
|
|
|
} |
1058
|
|
|
|
|
|
|
elsif ($number >= $mult{mega}) |
1059
|
|
|
|
|
|
|
{ |
1060
|
6
|
|
|
|
|
7
|
$unit = 'M'; |
1061
|
|
|
|
|
|
|
} |
1062
|
|
|
|
|
|
|
elsif ($number >= $mult{kilo}) |
1063
|
|
|
|
|
|
|
{ |
1064
|
3
|
|
|
|
|
5
|
$unit = 'K'; |
1065
|
|
|
|
|
|
|
} |
1066
|
|
|
|
|
|
|
else |
1067
|
|
|
|
|
|
|
{ |
1068
|
4
|
|
|
|
|
9
|
$unit = 'N'; |
1069
|
|
|
|
|
|
|
} |
1070
|
|
|
|
|
|
|
} |
1071
|
|
|
|
|
|
|
|
1072
|
|
|
|
|
|
|
# Based on unit, whether specified or determined above, divide the |
1073
|
|
|
|
|
|
|
# number and determine what suffix to use. |
1074
|
17
|
|
|
|
|
23
|
my $suffix = ""; |
1075
|
17
|
100
|
|
|
|
60
|
if ($unit eq 'G') |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
{ |
1077
|
2
|
|
|
|
|
3
|
$number /= $mult{giga}; |
1078
|
2
|
|
|
|
|
4
|
$suffix = $gsuff; |
1079
|
|
|
|
|
|
|
} |
1080
|
|
|
|
|
|
|
elsif ($unit eq 'M') |
1081
|
|
|
|
|
|
|
{ |
1082
|
6
|
|
|
|
|
9
|
$number /= $mult{mega}; |
1083
|
6
|
|
|
|
|
9
|
$suffix = $msuff; |
1084
|
|
|
|
|
|
|
} |
1085
|
|
|
|
|
|
|
elsif ($unit eq 'K') |
1086
|
|
|
|
|
|
|
{ |
1087
|
5
|
|
|
|
|
8
|
$number /= $mult{kilo}; |
1088
|
5
|
|
|
|
|
10
|
$suffix = $ksuff; |
1089
|
|
|
|
|
|
|
} |
1090
|
|
|
|
|
|
|
elsif ($unit ne 'N') |
1091
|
|
|
|
|
|
|
{ |
1092
|
0
|
|
|
|
|
0
|
croak "Invalid unit option"; |
1093
|
|
|
|
|
|
|
} |
1094
|
|
|
|
|
|
|
|
1095
|
|
|
|
|
|
|
# Format the number and add the suffix. |
1096
|
17
|
|
|
|
|
63
|
return $self->format_number($number, $options{precision}) . $suffix; |
1097
|
|
|
|
|
|
|
} |
1098
|
|
|
|
|
|
|
|
1099
|
|
|
|
|
|
|
##---------------------------------------------------------------------- |
1100
|
|
|
|
|
|
|
|
1101
|
|
|
|
|
|
|
=item unformat_number($formatted) |
1102
|
|
|
|
|
|
|
|
1103
|
|
|
|
|
|
|
Converts a string as returned by C, |
1104
|
|
|
|
|
|
|
C, or C, and returns the |
1105
|
|
|
|
|
|
|
corresponding value as a numeric scalar. Returns C if the |
1106
|
|
|
|
|
|
|
number does not contain any digits. Examples: |
1107
|
|
|
|
|
|
|
|
1108
|
|
|
|
|
|
|
unformat_number('USD 12.95') yields 12.95 |
1109
|
|
|
|
|
|
|
unformat_number('USD 12.00') yields 12 |
1110
|
|
|
|
|
|
|
unformat_number('foobar') yields undef |
1111
|
|
|
|
|
|
|
unformat_number('1234-567@.8') yields 1234567.8 |
1112
|
|
|
|
|
|
|
|
1113
|
|
|
|
|
|
|
The value of C is used to determine where to separate |
1114
|
|
|
|
|
|
|
the integer and decimal portions of the input. All other non-digit |
1115
|
|
|
|
|
|
|
characters, including but not limited to C and |
1116
|
|
|
|
|
|
|
C, are removed. |
1117
|
|
|
|
|
|
|
|
1118
|
|
|
|
|
|
|
If the number matches the pattern of C I there is a |
1119
|
|
|
|
|
|
|
``-'' character before any of the digits, then a negative number is |
1120
|
|
|
|
|
|
|
returned. |
1121
|
|
|
|
|
|
|
|
1122
|
|
|
|
|
|
|
If the number ends with the C, C, |
1123
|
|
|
|
|
|
|
C, C, C, or C |
1124
|
|
|
|
|
|
|
characters, then the number returned will be multiplied by the |
1125
|
|
|
|
|
|
|
appropriate multiple of 1024 (or if the base option is given, by the |
1126
|
|
|
|
|
|
|
multiple of that value) as appropriate. Examples: |
1127
|
|
|
|
|
|
|
|
1128
|
|
|
|
|
|
|
unformat_number("4K", base => 1024) yields 4096 |
1129
|
|
|
|
|
|
|
unformat_number("4K", base => 1000) yields 4000 |
1130
|
|
|
|
|
|
|
unformat_number("4KiB", base => 1024) yields 4096 |
1131
|
|
|
|
|
|
|
unformat_number("4G") yields 4294967296 |
1132
|
|
|
|
|
|
|
|
1133
|
|
|
|
|
|
|
=cut |
1134
|
|
|
|
|
|
|
|
1135
|
|
|
|
|
|
|
sub unformat_number |
1136
|
|
|
|
|
|
|
{ |
1137
|
19
|
|
|
19
|
1
|
1909
|
my ($self, $formatted, %options) = _get_self @_; |
1138
|
19
|
|
|
|
|
57
|
$self->_check_seps(); |
1139
|
19
|
100
|
|
|
|
75
|
return undef unless $formatted =~ /\d/; # require at least one digit |
1140
|
|
|
|
|
|
|
|
1141
|
|
|
|
|
|
|
# Regular expression for detecting decimal point |
1142
|
18
|
|
|
|
|
120
|
my $pt = qr/\Q$self->{decimal_point}\E/; |
1143
|
|
|
|
|
|
|
|
1144
|
|
|
|
|
|
|
# ru_RU locale has comma for decimal_point, but period for |
1145
|
|
|
|
|
|
|
# mon_decimal_point! But as long as thousands_sep is different |
1146
|
|
|
|
|
|
|
# from either, we can allow either decimal point. |
1147
|
18
|
0
|
33
|
|
|
123
|
if ($self->{mon_decimal_point} && |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
1148
|
|
|
|
|
|
|
$self->{decimal_point} ne $self->{mon_decimal_point} && |
1149
|
|
|
|
|
|
|
$self->{decimal_point} ne $self->{mon_thousands_sep} && |
1150
|
|
|
|
|
|
|
$self->{mon_decimal_point} ne $self->{thousands_sep}) |
1151
|
|
|
|
|
|
|
{ |
1152
|
0
|
|
|
|
|
0
|
$pt = qr/(?:\Q$self->{decimal_point}\E| |
1153
|
|
|
|
|
|
|
\Q$self->{mon_decimal_point}\E)/x; |
1154
|
|
|
|
|
|
|
} |
1155
|
|
|
|
|
|
|
|
1156
|
|
|
|
|
|
|
# Detect if it ends with one of the kilo / mega / giga suffixes. |
1157
|
18
|
|
|
|
|
180
|
my $kp = ($formatted =~ |
1158
|
|
|
|
|
|
|
s/\s*($self->{kilo_suffix}|$self->{kibi_suffix})\s*$//); |
1159
|
18
|
|
|
|
|
121
|
my $mp = ($formatted =~ |
1160
|
|
|
|
|
|
|
s/\s*($self->{mega_suffix}|$self->{mebi_suffix})\s*$//); |
1161
|
18
|
|
|
|
|
110
|
my $gp = ($formatted =~ |
1162
|
|
|
|
|
|
|
s/\s*($self->{giga_suffix}|$self->{gibi_suffix})\s*$//); |
1163
|
18
|
|
|
|
|
53
|
my %mult = _get_multipliers($options{base}); |
1164
|
|
|
|
|
|
|
|
1165
|
|
|
|
|
|
|
# Split number into integer and decimal parts |
1166
|
14
|
|
|
|
|
73
|
my ($integer, $decimal, @cruft) = split($pt, $formatted); |
1167
|
14
|
50
|
|
|
|
39
|
croak "Only one decimal separator permitted" |
1168
|
|
|
|
|
|
|
if @cruft; |
1169
|
|
|
|
|
|
|
|
1170
|
|
|
|
|
|
|
# It's negative if the first non-digit character is a - |
1171
|
14
|
100
|
|
|
|
36
|
my $sign = $formatted =~ /^\D*-/ ? -1 : 1; |
1172
|
14
|
|
|
|
|
40
|
my($before_re, $after_re) = split /x/, $self->{neg_format}, 2; |
1173
|
14
|
100
|
|
|
|
102
|
$sign = -1 if $formatted =~ /\Q$before_re\E(.+)\Q$after_re\E/; |
1174
|
|
|
|
|
|
|
|
1175
|
|
|
|
|
|
|
# Strip out all non-digits from integer and decimal parts |
1176
|
14
|
50
|
|
|
|
30
|
$integer = '' unless defined $integer; |
1177
|
14
|
100
|
|
|
|
24
|
$decimal = '' unless defined $decimal; |
1178
|
14
|
|
|
|
|
44
|
$integer =~ s/\D//g; |
1179
|
14
|
|
|
|
|
19
|
$decimal =~ s/\D//g; |
1180
|
|
|
|
|
|
|
|
1181
|
|
|
|
|
|
|
# Join back up, using period, and add 0 to make Perl think it's a number |
1182
|
14
|
|
|
|
|
44
|
my $number = join('.', $integer, $decimal) + 0; |
1183
|
14
|
100
|
|
|
|
26
|
$number = -$number if $sign < 0; |
1184
|
|
|
|
|
|
|
|
1185
|
|
|
|
|
|
|
# Scale the number if it ended in kilo or mega suffix. |
1186
|
14
|
100
|
|
|
|
107
|
$number *= $mult{kilo} if $kp; |
1187
|
14
|
100
|
|
|
|
26
|
$number *= $mult{mega} if $mp; |
1188
|
14
|
100
|
|
|
|
26
|
$number *= $mult{giga} if $gp; |
1189
|
|
|
|
|
|
|
|
1190
|
14
|
|
|
|
|
98
|
return $number; |
1191
|
|
|
|
|
|
|
} |
1192
|
|
|
|
|
|
|
|
1193
|
|
|
|
|
|
|
###--------------------------------------------------------------------- |
1194
|
|
|
|
|
|
|
|
1195
|
|
|
|
|
|
|
=back |
1196
|
|
|
|
|
|
|
|
1197
|
|
|
|
|
|
|
=head1 CAVEATS |
1198
|
|
|
|
|
|
|
|
1199
|
|
|
|
|
|
|
Some systems, notably OpenBSD, may have incomplete locale support. |
1200
|
|
|
|
|
|
|
Using this module together with L in OpenBSD may therefore |
1201
|
|
|
|
|
|
|
not produce the intended results. |
1202
|
|
|
|
|
|
|
|
1203
|
|
|
|
|
|
|
=head1 BUGS |
1204
|
|
|
|
|
|
|
|
1205
|
|
|
|
|
|
|
No known bugs at this time. Report bugs using the CPAN request |
1206
|
|
|
|
|
|
|
tracker at L |
1207
|
|
|
|
|
|
|
or by email to the author. |
1208
|
|
|
|
|
|
|
|
1209
|
|
|
|
|
|
|
=head1 AUTHOR |
1210
|
|
|
|
|
|
|
|
1211
|
|
|
|
|
|
|
William R. Ward, SwPrAwM@cpan.org (remove "SPAM" before sending email, |
1212
|
|
|
|
|
|
|
leaving only my initials) |
1213
|
|
|
|
|
|
|
|
1214
|
|
|
|
|
|
|
=head1 SEE ALSO |
1215
|
|
|
|
|
|
|
|
1216
|
|
|
|
|
|
|
perl(1). |
1217
|
|
|
|
|
|
|
|
1218
|
|
|
|
|
|
|
=cut |
1219
|
|
|
|
|
|
|
|
1220
|
|
|
|
|
|
|
1; |