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