1Format(3)             User Contributed Perl Documentation            Format(3)
2
3
4

NAME

6       Number::Format - Perl extension for formatting numbers
7

SYNOPSIS

9         use Number::Format;
10         my $x = new Number::Format %args;
11         $formatted = $x->round($number, $precision);
12         $formatted = $x->format_number($number, $precision, $trailing_zeroes);
13         $formatted = $x->format_negative($number, $picture);
14         $formatted = $x->format_picture($number, $picture);
15         $formatted = $x->format_price($number, $precision, $symbol);
16         $formatted = $x->format_bytes($number, $precision);
17         $number    = $x->unformat_number($formatted);
18
19         use Number::Format qw(:subs);
20         $formatted = round($number, $precision);
21         $formatted = format_number($number, $precision, $trailing_zeroes);
22         $formatted = format_negative($number, $picture);
23         $formatted = format_picture($number, $picture);
24         $formatted = format_price($number, $precision, $symbol);
25         $formatted = format_bytes($number, $precision);
26         $number    = unformat_number($formatted);
27

REQUIRES

29       Perl, version 5.8 or higher.
30
31       POSIX.pm to determine locale settings.
32
33       Carp.pm is used for some error reporting.
34

DESCRIPTION

36       These functions provide an easy means of formatting numbers in a manner
37       suitable for displaying to the user.
38
39       There are two ways to use this package.  One is to declare an object of
40       type Number::Format, which you can think of as a formatting engine.
41       The various functions defined here are provided as object methods.  The
42       constructor "new()" can be used to set the parameters of the formatting
43       engine.  Valid parameters are:
44
45         THOUSANDS_SEP     - character inserted between groups of 3 digits
46         DECIMAL_POINT     - character separating integer and fractional parts
47         MON_THOUSANDS_SEP - like THOUSANDS_SEP, but used for format_price
48         MON_DECIMAL_POINT - like DECIMAL_POINT, but used for format_price
49         INT_CURR_SYMBOL   - character(s) denoting currency (see format_price())
50         DECIMAL_DIGITS    - number of digits to the right of dec point (def 2)
51         DECIMAL_FILL      - boolean; whether to add zeroes to fill out decimal
52         NEG_FORMAT        - format to display negative numbers (def ``-x'')
53         KILO_SUFFIX       - suffix to add when format_bytes formats kilobytes (trad)
54         MEGA_SUFFIX       -    "    "  "    "        "         "    megabytes (trad)
55         GIGA_SUFFIX       -    "    "  "    "        "         "    gigabytes (trad)
56         KIBI_SUFFIX       - suffix to add when format_bytes formats kibibytes (iec)
57         MEBI_SUFFIX       -    "    "  "    "        "         "    mebibytes (iec)
58         GIBI_SUFFIX       -    "    "  "    "        "         "    gibibytes (iec)
59
60       They may be specified in upper or lower case, with or without a leading
61       hyphen ( - ).
62
63       If "THOUSANDS_SEP" is set to the empty string, format_number will not
64       insert any separators.
65
66       The defaults for "THOUSANDS_SEP", "DECIMAL_POINT", "MON_THOUSANDS_SEP",
67       "MON_DECIMAL_POINT", and "INT_CURR_SYMBOL" come from the POSIX locale
68       information (see perllocale).  If your POSIX locale does not provide
69       "MON_THOUSANDS_SEP" and/or "MON_DECIMAL_POINT" fields, then the
70       "THOUSANDS_SEP" and/or "DECIMAL_POINT" values are used for those
71       parameters.  Formerly, POSIX was optional but this caused problems in
72       some cases, so it is now required.  If this causes you hardship, please
73       contact the author of this package at <SwPrAwM@cpan.org> (remove "SPAM"
74       to get correct email address) for help.
75
76       If any of the above parameters are not specified when you invoke
77       "new()", then the values are taken from package global variables of the
78       same name (e.g.  $DECIMAL_POINT is the default for the "DECIMAL_POINT"
79       parameter).  If you use the ":vars" keyword on your "use
80       Number::Format" line (see non-object-oriented example below) you will
81       import those variables into your namesapce and can assign values as if
82       they were your own local variables.  The default values for all the
83       parameters are:
84
85         THOUSANDS_SEP     = ','
86         DECIMAL_POINT     = '.'
87         MON_THOUSANDS_SEP = ','
88         MON_DECIMAL_POINT = '.'
89         INT_CURR_SYMBOL   = 'USD'
90         DECIMAL_DIGITS    = 2
91         DECIMAL_FILL      = 0
92         NEG_FORMAT        = '-x'
93         KILO_SUFFIX       = 'K'
94         MEGA_SUFFIX       = 'M'
95         GIGA_SUFFIX       = 'G'
96         KIBI_SUFFIX       = 'KiB'
97         MEBI_SUFFIX       = 'MiB'
98         GIBI_SUFFIX       = 'GiB'
99
100       Note however that when you first call one of the functions in this
101       module without using the object-oriented interface, further setting of
102       those global variables will have no effect on non-OO calls.  It is
103       recommended that you use the object-oriented interface instead for
104       fewer headaches and a cleaner design.
105
106       The "DECIMAL_FILL" and "DECIMAL_DIGITS" values are not set by the
107       Locale system, but are definable by the user.  They affect the output
108       of "format_number()".  Setting "DECIMAL_DIGITS" is like giving that
109       value as the $precision argument to that function.  Setting
110       "DECIMAL_FILL" to a true value causes "format_number()" to append
111       zeroes to the right of the decimal digits until the length is the
112       specified number of digits.
113
114       "NEG_FORMAT" is only used by "format_negative()" and is a string
115       containing the letter 'x', where that letter will be replaced by a
116       positive representation of the number being passed to that function.
117       "format_number()" and "format_price()" utilize this feature by calling
118       "format_negative()" if the number was less than 0.
119
120       "KILO_SUFFIX", "MEGA_SUFFIX", and "GIGA_SUFFIX" are used by
121       "format_bytes()" when the value is over 1024, 1024*1024, or
122       1024*1024*1024, respectively.  The default values are "K", "M", and
123       "G".  These apply in the default "traditional" mode only.  Note: TERA
124       or higher are not implemented because of integer overflows on 32-bit
125       systems.
126
127       "KIBI_SUFFIX", "MEBI_SUFFIX", and "GIBI_SUFFIX" are used by
128       "format_bytes()" when the value is over 1024, 1024*1024, or
129       1024*1024*1024, respectively.  The default values are "KiB", "MiB", and
130       "GiB".  These apply in the "iso60027"" mode only.  Note: TEBI or higher
131       are not implemented because of integer overflows on 32-bit systems.
132
133       The only restrictions on "DECIMAL_POINT" and "THOUSANDS_SEP" are that
134       they must not be digits, must not be identical, and must each be one
135       character.  There are no restrictions on "INT_CURR_SYMBOL".
136
137       For example, a German user might include this in their code:
138
139         use Number::Format;
140         my $de = new Number::Format(-thousands_sep   => '.',
141                                     -decimal_point   => ',',
142                                     -int_curr_symbol => 'DEM');
143         my $formatted = $de->format_number($number);
144
145       Or, if you prefer not to use the object oriented interface, you can do
146       this instead:
147
148         use Number::Format qw(:subs :vars);
149         $THOUSANDS_SEP   = '.';
150         $DECIMAL_POINT   = ',';
151         $INT_CURR_SYMBOL = 'DEM';
152         my $formatted = format_number($number);
153

EXPORTS

155       Nothing is exported by default.  To export the functions or the global
156       variables defined herein, specify the function name(s) on the import
157       list of the "use Number::Format" statement.  To export all functions
158       defined herein, use the special tag ":subs".  To export the variables,
159       use the special tag ":vars"; to export both subs and vars you can use
160       the tag ":all".
161

METHODS

163       new( %args )
164           Creates a new Number::Format object.  Valid keys for %args are any
165           of the parameters described above.  Keys may be in all uppercase or
166           all lowercase, and may optionally be preceded by a hyphen (-)
167           character.  Example:
168
169             my $de = new Number::Format(-thousands_sep   => '.',
170                                         -decimal_point   => ',',
171                                         -int_curr_symbol => 'DEM');
172
173       round($number, $precision)
174           Rounds the number to the specified precision.  If $precision is
175           omitted, the value of the "DECIMAL_DIGITS" parameter is used
176           (default value 2).  Both input and output are numeric (the function
177           uses math operators rather than string manipulation to do its job),
178           The value of $precision may be any integer, positive or negative.
179           Examples:
180
181             round(3.14159)       yields    3.14
182             round(3.14159, 4)    yields    3.1416
183             round(42.00, 4)      yields    42
184             round(1234, -2)      yields    1200
185
186           Since this is a mathematical rather than string oriented function,
187           there will be no trailing zeroes to the right of the decimal point,
188           and the "DECIMAL_POINT" and "THOUSANDS_SEP" variables are ignored.
189           To format your number using the "DECIMAL_POINT" and "THOUSANDS_SEP"
190           variables, use "format_number()" instead.
191
192       format_number($number, $precision, $trailing_zeroes)
193           Formats a number by adding "THOUSANDS_SEP" between each set of 3
194           digits to the left of the decimal point, substituting
195           "DECIMAL_POINT" for the decimal point, and rounding to the
196           specified precision using "round()".  Note that $precision is a
197           maximum precision specifier; trailing zeroes will only appear in
198           the output if $trailing_zeroes is provided, or the parameter
199           "DECIMAL_FILL" is set, with a value that is true (not zero, undef,
200           or the empty string).  If $precision is omitted, the value of the
201           "DECIMAL_DIGITS" parameter (default value of 2) is used.
202
203           If the value is too large or great to work with as a regular
204           number, but instead must be shown in scientific notation, returns
205           that number in scientific notation without further formatting.
206
207           Examples:
208
209             format_number(12345.6789)             yields   '12,345.68'
210             format_number(123456.789, 2)          yields   '123,456.79'
211             format_number(1234567.89, 2)          yields   '1,234,567.89'
212             format_number(1234567.8, 2)           yields   '1,234,567.8'
213             format_number(1234567.8, 2, 1)        yields   '1,234,567.80'
214             format_number(1.23456789, 6)          yields   '1.234568'
215             format_number("0.000020000E+00", 7);' yields   '2e-05'
216
217           Of course the output would have your values of "THOUSANDS_SEP" and
218           "DECIMAL_POINT" instead of ',' and '.' respectively.
219
220       format_negative($number, $picture)
221           Formats a negative number.  Picture should be a string that
222           contains the letter "x" where the number should be inserted.  For
223           example, for standard negative numbers you might use ``"-x"'',
224           while for accounting purposes you might use ``"(x)"''.  If the
225           specified number begins with a ``-'' character, that will be
226           removed before formatting, but formatting will occur whether or not
227           the number is negative.
228
229       format_picture($number, $picture)
230           Returns a string based on $picture with the "#" characters replaced
231           by digits from $number.  If the length of the integer part of
232           $number is too large to fit, the "#" characters are replaced with
233           asterisks ("*") instead.  Examples:
234
235             format_picture(100.023, 'USD ##,###.##')   yields   'USD    100.02'
236             format_picture(1000.23, 'USD ##,###.##')   yields   'USD  1,000.23'
237             format_picture(10002.3, 'USD ##,###.##')   yields   'USD 10,002.30'
238             format_picture(100023,  'USD ##,###.##')   yields   'USD **,***.**'
239             format_picture(1.00023, 'USD #.###,###')   yields   'USD 1.002,300'
240
241           The comma (,) and period (.) you see in the picture examples should
242           match the values of "THOUSANDS_SEP" and "DECIMAL_POINT",
243           respectively, for proper operation.  However, the "THOUSANDS_SEP"
244           characters in $picture need not occur every three digits; the only
245           use of that variable by this function is to remove leading commas
246           (see the first example above).  There may not be more than one
247           instance of "DECIMAL_POINT" in $picture.
248
249           The value of "NEG_FORMAT" is used to determine how negative numbers
250           are displayed.  The result of this is that the output of this
251           function my have unexpected spaces before and/or after the number.
252           This is necessary so that positive and negative numbers are
253           formatted into a space the same size.  If you are only using
254           positive numbers and want to avoid this problem, set NEG_FORMAT to
255           "x".
256
257       format_price($number, $precision, $symbol)
258           Returns a string containing $number formatted similarly to
259           "format_number()", except that the decimal portion may have
260           trailing zeroes added to make it be exactly $precision characters
261           long, and the currency string will be prefixed.
262
263           The $symbol attribute may be one of "INT_CURR_SYMBOL" or
264           "CURRENCY_SYMBOL" (case insensitive) to use the value of that
265           attribute of the object, or a string containing the symbol to be
266           used.  The default is "INT_CURR_SYMBOL" if this argument is
267           undefined or not given; if set to the empty string, or if set to
268           undef and the "INT_CURR_SYMBOL" attribute of the object is the
269           empty string, no currency will be added.
270
271           If $precision is not provided, the default of 2 will be used.
272           Examples:
273
274             format_price(12.95)   yields   'USD 12.95'
275             format_price(12)      yields   'USD 12.00'
276             format_price(12, 3)   yields   '12.000'
277
278           The third example assumes that "INT_CURR_SYMBOL" is the empty
279           string.
280
281       format_bytes($number, %options)
282       format_bytes($number, $precision)  # deprecated
283           Returns a string containing $number formatted similarly to
284           "format_number()", except that large numbers may be abbreviated by
285           adding a suffix to indicate 1024, 1,048,576, or 1,073,741,824
286           bytes.  Suffix may be the traditional K, M, or G (default); or the
287           IEC standard 60027 "KiB," "MiB," or "GiB" depending on the "mode"
288           option.
289
290           Negative values will result in an error.
291
292           The second parameter can be either a hash that sets options, or a
293           number.  Using a number here is deprecated and will generate a
294           warning; early versions of Number::Format only allowed a numeric
295           value.  A future release of Number::Format will change this warning
296           to an error.  New code should use a hash instead to set options.
297           If it is a number this sets the value of the "precision" option.
298
299           Valid options are:
300
301           precision
302               Set the precision for displaying numbers.  If not provided, a
303               default of 2 will be used.  Examples:
304
305                 format_bytes(12.95)                   yields   '12.95'
306                 format_bytes(12.95, precision => 0)   yields   '13'
307                 format_bytes(2048)                    yields   '2K'
308                 format_bytes(2048, mode => "iec")     yields   '2KiB'
309                 format_bytes(9999999)                 yields   '9.54M'
310                 format_bytes(9999999, precision => 1) yields   '9.5M'
311
312           unit
313               Sets the default units used for the results.  The default is to
314               determine this automatically in order to minimize the length of
315               the string.  In other words, numbers greater than or equal to
316               1024 (or other number given by the 'base' option, q.v.) will be
317               divided by 1024 and $KILO_SUFFIX or $KIBI_SUFFIX added; if
318               greater than or equal to 1048576 (1024*1024), it will be
319               divided by 1048576 and $MEGA_SUFFIX or $MEBI_SUFFIX appended to
320               the end; etc.
321
322               However if a value is given for "unit" it will use that value
323               instead.  The first letter (case-insensitive) of the value
324               given indicates the threshhold for conversion; acceptable
325               values are G (for giga/gibi), M (for mega/mebi), K (for
326               kilo/kibi), or A (for automatic, the default).  For example:
327
328                 format_bytes(1048576, unit => 'K') yields     '1,024K'
329                                                    instead of '1M'
330
331               Note that the valid values to this option do not vary even when
332               the suffix configuration variables have been changed.
333
334           base
335               Sets the number at which the $KILO_SUFFIX is added.  Default is
336               1024.  Set to any value; the only other useful value is
337               probably 1000, as hard disk manufacturers use that number to
338               make their disks sound bigger than they really are.
339
340               If the mode (see below) is set to "iec" or "iec60027" then
341               setting the base option results in an error.
342
343           mode
344               Traditionally, bytes have been given in SI (metric) units such
345               as "kilo" and "mega" even though they represent powers of 2
346               (1024, etc.)  rather than powers of 10 (1000, etc.)  This
347               "binary prefix" causes much confusion in consumer products
348               where "GB" may mean either 1,048,576 or 1,000,000, for example.
349               The International Electrotechnical Commission has created
350               standard IEC 60027 to introduce prefixes Ki, Mi, Gi, etc.
351               ("kibibytes," "mebibytes," "gibibytes," etc.) to remove this
352               confusion.  Specify a mode option with either "traditional" or
353               "iec60027" (or abbreviate as "trad" or "iec") to indicate which
354               type of binary prefix you want format_bytes to use.  For
355               backward compatibility, "traditional" is the default.  See
356               http://en.wikipedia.org/wiki/Binary_prefix for more
357               information.
358
359       unformat_number($formatted)
360           Converts a string as returned by "format_number()",
361           "format_price()", or "format_picture()", and returns the
362           corresponding value as a numeric scalar.  Returns "undef" if the
363           number does not contain any digits.  Examples:
364
365             unformat_number('USD 12.95')   yields   12.95
366             unformat_number('USD 12.00')   yields   12
367             unformat_number('foobar')      yields   undef
368             unformat_number('1234-567@.8') yields   1234567.8
369
370           The value of "DECIMAL_POINT" is used to determine where to separate
371           the integer and decimal portions of the input.  All other non-digit
372           characters, including but not limited to "INT_CURR_SYMBOL" and
373           "THOUSANDS_SEP", are removed.
374
375           If the number matches the pattern of "NEG_FORMAT" or there is a
376           ``-'' character before any of the digits, then a negative number is
377           returned.
378
379           If the number ends with the "KILO_SUFFIX", "KIBI_SUFFIX",
380           "MEGA_SUFFIX", "MEBI_SUFFIX", "GIGA_SUFFIX", or "GIBI_SUFFIX"
381           characters, then the number returned will be multiplied by the
382           appropriate multiple of 1024 (or if the base option is given, by
383           the multiple of that value) as appropriate.  Examples:
384
385             unformat_number("4K", base => 1024)   yields  4096
386             unformat_number("4K", base => 1000)   yields  4000
387             unformat_number("4KiB", base => 1024) yields  4096
388             unformat_number("4G")                 yields  4294967296
389

CAVEATS

391       Some systems, notably OpenBSD, may have incomplete locale support.
392       Using this module together with setlocale(3) in OpenBSD may therefore
393       not produce the intended results.
394

BUGS

396       No known bugs at this time.  Report bugs using the CPAN request tracker
397       at https://rt.cpan.org/NoAuth/Bugs.html?Dist=Number-Format
398       <https://rt.cpan.org/NoAuth/Bugs.html?Dist=Number-Format> or by email
399       to the author.
400

AUTHOR

402       William R. Ward, SwPrAwM@cpan.org (remove "SPAM" before sending email,
403       leaving only my initials)
404

SEE ALSO

406       perl(1).
407
408
409
410perl v5.12.0                      2009-09-25                         Format(3)
Impressum