Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Number::Format.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "Format 3"
132.TH Format 3 "2002-08-27" "perl v5.8.0" "User Contributed Perl Documentation"
133.SH "NAME"
134Number::Format \- Perl extension for formatting numbers
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 9
138\& use Number::Format;
139\& my $x = new Number::Format %args;
140\& $formatted = $x->round($number, $precision);
141\& $formatted = $x->format_number($number, $precision, $trailing_zeroes);
142\& $formatted = $x->format_negative($number, $picture);
143\& $formatted = $x->format_picture($number, $picture);
144\& $formatted = $x->format_price($number, $precision);
145\& $formatted = $x->format_bytes($number, $precision);
146\& $number = $x->unformat_number($formatted);
147.Ve
148.PP
149.Vb 8
150\& use Number::Format qw(:subs);
151\& $formatted = round($number, $precision);
152\& $formatted = format_number($number, $precision, $trailing_zeroes);
153\& $formatted = format_negative($number, $picture);
154\& $formatted = format_picture($number, $picture);
155\& $formatted = format_price($number, $precision);
156\& $formatted = format_bytes($number, $precision);
157\& $number = unformat_number($formatted);
158.Ve
159.SH "REQUIRES"
160.IX Header "REQUIRES"
161Perl, version 5.003 or higher.
162.PP
163\&\s-1POSIX\s0.pm will be used if present to determine locale settings.
164.PP
165Carp.pm is used for some error reporting.
166.SH "DESCRIPTION"
167.IX Header "DESCRIPTION"
168These functions provide an easy means of formatting numbers in a
169manner suitable for displaying to the user.
170.PP
171There are two ways to use this package. One is to declare an object
172of type Number::Format, which you can think of as a formatting engine.
173The various functions defined here are provided as object methods.
174The constructor \f(CW\*(C`new()\*(C'\fR can be used to set the parameters of the
175formatting engine. Valid parameters are:
176.PP
177.Vb 11
178\& THOUSANDS_SEP - character inserted between groups of 3 digits
179\& DECIMAL_POINT - character separating integer and fractional parts
180\& MON_THOUSANDS_SEP - like THOUSANDS_SEP, but used for format_price
181\& MON_DECIMAL_POINT - like DECIMAL_POINT, but used for format_price
182\& INT_CURR_SYMBOL - character(s) denoting currency (see format_price())
183\& DECIMAL_DIGITS - number of digits to the right of dec point (def 2)
184\& DECIMAL_FILL - boolean; whether to add zeroes to fill out decimal
185\& NEG_FORMAT - format to display negative numbers (def ``-x'')
186\& KILO_SUFFIX - suffix to add when format_bytes formats kilobytes
187\& MEGA_SUFFIX - " " " " " " megabytes
188\& GIGA_SUFFIX - " " " " " " gigabytes
189.Ve
190.PP
191They may be specified in upper or lower case, with or without a
192leading hyphen ( \- ).
193.PP
194The defaults for \f(CW\*(C`THOUSANDS_SEP\*(C'\fR, \f(CW\*(C`DECIMAL_POINT\*(C'\fR,
195\&\f(CW\*(C`MON_THOUSANDS_SEP\*(C'\fR, \f(CW\*(C`MON_DECIMAL_POINT\*(C'\fR, and \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR
196come from the \s-1POSIX\s0 locale information (see perllocale), if
197available. If your \s-1POSIX\s0 locale does not provide \f(CW\*(C`MON_THOUSANDS_SEP\*(C'\fR
198and/or \f(CW\*(C`MON_DECIMAL_POINT\*(C'\fR fields, then the \f(CW\*(C`THOUSANDS_SEP\*(C'\fR and/or
199\&\f(CW\*(C`DECIMAL_POINT\*(C'\fR values are used for those parameters. Some systems
200(e.g. Win32 ports of Perl) do not include \s-1POSIX\s0 support. In those
201systems, the \s-1POSIX\s0 system is bypassed.
202.PP
203If any of the above parameters are not specified when you invoke
204\&\f(CW\*(C`new()\*(C'\fR, then the values are taken from package global variables of
205the same name (e.g. \f(CW$DECIMAL_POINT\fR is the default for the
206\&\f(CW\*(C`DECIMAL_POINT\*(C'\fR parameter). If you use the \f(CW\*(C`:vars\*(C'\fR keyword on your
207\&\f(CW\*(C`use Number::Format\*(C'\fR line (see non-object-oriented example below) you
208will import those variables into your namesapce and can assign values
209as if they were your own local variables. The default values for all
210the parameters are:
211.PP
212.Vb 11
213\& THOUSANDS_SEP = ','
214\& DECIMAL_POINT = '.'
215\& MON_THOUSANDS_SEP = ','
216\& MON_DECIMAL_POINT = '.'
217\& INT_CURR_SYMBOL = 'USD'
218\& DECIMAL_DIGITS = 2
219\& DECIMAL_FILL = 0
220\& NEG_FORMAT = '-x'
221\& KILO_SUFFIX = 'K'
222\& MEGA_SUFFIX = 'M'
223\& GIGA_SUFFIX = 'G'
224.Ve
225.PP
226Note however that when you first call one of the functions in this
227module \fIwithout\fR using the object-oriented interface, further setting
228of those global variables will have no effect on non-OO calls. It is
229recommended that you use the object-oriented interface instead for
230fewer headaches and a cleaner design.
231.PP
232The \f(CW\*(C`DECIMAL_FILL\*(C'\fR and \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR values are not set by the
233Locale system, but are definable by the user. They affect the output
234of \f(CW\*(C`format_number()\*(C'\fR. Setting \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR is like giving that
235value as the \f(CW$precision\fR argument to that function. Setting
236\&\f(CW\*(C`DECIMAL_FILL\*(C'\fR to a true value causes \f(CW\*(C`format_number()\*(C'\fR to append
237zeroes to the right of the decimal digits until the length is the
238specified number of digits.
239.PP
240\&\f(CW\*(C`NEG_FORMAT\*(C'\fR is only used by \f(CW\*(C`format_negative()\*(C'\fR and is a string
241containing the letter 'x', where that letter will be replaced by a
242positive representation of the number being passed to that function.
243\&\f(CW\*(C`format_number()\*(C'\fR and \f(CW\*(C`format_price()\*(C'\fR utilize this feature by
244calling \f(CW\*(C`format_negative()\*(C'\fR if the number was less than 0.
245.PP
246\&\f(CW\*(C`KILO_SUFFIX\*(C'\fR, \f(CW\*(C`MEGA_SUFFIX\*(C'\fR, and \f(CW\*(C`GIGA_SUFFIX\*(C'\fR are used by
247\&\f(CW\*(C`format_bytes()\*(C'\fR when the value is over 1024, 1024*1024, or
2481024*1024*1024, respectively. The default values are \*(L"K\*(R", \*(L"M\*(R", and
249\&\*(L"G\*(R". Note: we can't do \s-1TERA\s0 because of integer overflows on 32\-bit
250systems.
251.PP
252The only restrictions on \f(CW\*(C`DECIMAL_POINT\*(C'\fR and \f(CW\*(C`THOUSANDS_SEP\*(C'\fR are that
253they must not be digits, must not be identical, and must each be one
254character. There are no restrictions on \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR.
255.PP
256For example, a German user might include this in their code:
257.PP
258.Vb 5
259\& use Number::Format;
260\& my $de = new Number::Format(-thousands_sep => '.',
261\& -decimal_point => ',',
262\& -int_curr_symbol => 'DEM');
263\& my $formatted = $de->format_number($number);
264.Ve
265.PP
266Or, if you prefer not to use the object oriented interface, you can do
267this instead:
268.PP
269.Vb 5
270\& use Number::Format qw(:subs :vars);
271\& $THOUSANDS_SEP = '.';
272\& $DECIMAL_POINT = ',';
273\& $INT_CURR_SYMBOL = 'DEM';
274\& my $formatted = format_number($number);
275.Ve
276.SH "EXPORTS"
277.IX Header "EXPORTS"
278Nothing is exported by default. To export the functions or the global
279variables defined herein, specify the function name(s) on the import
280list of the \f(CW\*(C`use Number::Format\*(C'\fR statement. To export all functions
281defined herein, use the special tag \f(CW\*(C`:subs\*(C'\fR. To export the
282variables, use the special tag \f(CW\*(C`:vars\*(C'\fR; to export both subs and vars
283you can use the tag \f(CW\*(C`:all\*(C'\fR.
284.SH "METHODS"
285.IX Header "METHODS"
286.ie n .IP "new( %args )" 4
287.el .IP "new( \f(CW%args\fR )" 4
288.IX Item "new( %args )"
289Creates a new Number::Format object. Valid keys for \f(CW%args\fR are any of
290the parameters described above. Keys may be in all uppercase or all
291lowercase, and may optionally be preceded by a hyphen (\-) character.
292Example:
293.Sp
294.Vb 3
295\& my $de = new Number::Format(-thousands_sep => '.',
296\& -decimal_point => ',',
297\& -int_curr_symbol => 'DEM');
298.Ve
299.ie n .IP "round($number, $precision)" 4
300.el .IP "round($number, \f(CW$precision\fR)" 4
301.IX Item "round($number, $precision)"
302Rounds the number to the specified precision. If \f(CW$precision\fR is
303omitted, the value of the \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR parameter is used (default
304value 2). Both input and output are numeric (the function uses math
305operators rather than string manipulation to do its job), The value of
306\&\f(CW$precision\fR may be any integer, positive or negative. Examples:
307.Sp
308.Vb 4
309\& round(3.14159) yields 3.14
310\& round(3.14159, 4) yields 3.1416
311\& round(42.00, 4) yields 42
312\& round(1234, -2) yields 1200
313.Ve
314.Sp
315Since this is a mathematical rather than string oriented function,
316there will be no trailing zeroes to the right of the decimal point,
317and the \f(CW\*(C`DECIMAL_POINT\*(C'\fR and \f(CW\*(C`THOUSANDS_SEP\*(C'\fR variables are ignored.
318To format your number using the \f(CW\*(C`DECIMAL_POINT\*(C'\fR and \f(CW\*(C`THOUSANDS_SEP\*(C'\fR
319variables, use \f(CW\*(C`format_number()\*(C'\fR instead.
320.ie n .IP "format_number($number, $precision\fR, \f(CW$trailing_zeroes)" 4
321.el .IP "format_number($number, \f(CW$precision\fR, \f(CW$trailing_zeroes\fR)" 4
322.IX Item "format_number($number, $precision, $trailing_zeroes)"
323Formats a number by adding \f(CW\*(C`THOUSANDS_SEP\*(C'\fR between each set of 3
324digits to the left of the decimal point, substituting \f(CW\*(C`DECIMAL_POINT\*(C'\fR
325for the decimal point, and rounding to the specified precision using
326\&\f(CW\*(C`round()\*(C'\fR. Note that \f(CW$precision\fR is a \fImaximum\fR precision
327specifier; trailing zeroes will only appear in the output if
328\&\f(CW$trailing_zeroes\fR is provided, or the parameter \f(CW\*(C`DECIMAL_FILL\*(C'\fR is
329set, with a value that is true (not zero, undef, or the empty string).
330If \f(CW$precision\fR is omitted, the value of the \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR
331parameter (default value of 2) is used. Examples:
332.Sp
333.Vb 6
334\& format_number(12345.6789) yields '12,345.68'
335\& format_number(123456.789, 2) yields '123,456.79'
336\& format_number(1234567.89, 2) yields '1,234,567.89'
337\& format_number(1234567.8, 2) yields '1,234,567.8'
338\& format_number(1234567.8, 2, 1) yields '1,234,567.80'
339\& format_number(1.23456789, 6) yields '1.234568'
340.Ve
341.Sp
342Of course the output would have your values of \f(CW\*(C`THOUSANDS_SEP\*(C'\fR and
343\&\f(CW\*(C`DECIMAL_POINT\*(C'\fR instead of ',' and '.' respectively.
344.ie n .IP "format_negative($number, $picture)" 4
345.el .IP "format_negative($number, \f(CW$picture\fR)" 4
346.IX Item "format_negative($number, $picture)"
347Formats a negative number. Picture should be a string that contains
348the letter \f(CW\*(C`x\*(C'\fR where the number should be inserted. For example, for
349standard negative numbers you might use ``\f(CW\*(C`\-x\*(C'\fR'', while for
350accounting purposes you might use ``\f(CW\*(C`(x)\*(C'\fR''. If the specified number
351begins with a ``\-'' character, that will be removed before formatting,
352but formatting will occur whether or not the number is negative.
353.ie n .IP "format_picture($number, $picture)" 4
354.el .IP "format_picture($number, \f(CW$picture\fR)" 4
355.IX Item "format_picture($number, $picture)"
356Returns a string based on \f(CW$picture\fR with the \f(CW\*(C`#\*(C'\fR characters
357replaced by digits from \f(CW$number\fR. If the length of the integer part
358of \f(CW$number\fR is too large to fit, the \f(CW\*(C`#\*(C'\fR characters are replaced with
359asterisks (\f(CW\*(C`*\*(C'\fR) instead. Examples:
360.Sp
361.Vb 5
362\& format_picture(100.023, 'USD ##,###.##') yields 'USD 100.02'
363\& format_picture(1000.23, 'USD ##,###.##') yields 'USD 1,000.23'
364\& format_picture(10002.3, 'USD ##,###.##') yields 'USD 10,002.30'
365\& format_picture(100023, 'USD ##,###.##') yields 'USD **,***.**'
366\& format_picture(1.00023, 'USD #.###,###') yields 'USD 1.002,300'
367.Ve
368.Sp
369The comma (,) and period (.) you see in the picture examples should
370match the values of \f(CW\*(C`THOUSANDS_SEP\*(C'\fR and \f(CW\*(C`DECIMAL_POINT\*(C'\fR,
371respectively, for proper operation. However, the \f(CW\*(C`THOUSANDS_SEP\*(C'\fR
372characters in \f(CW$picture\fR need not occur every three digits; the
373\&\fIonly\fR use of that variable by this function is to remove leading
374commas (see the first example above). There may not be more than one
375instance of \f(CW\*(C`DECIMAL_POINT\*(C'\fR in \f(CW$picture\fR.
376.Sp
377The value of \f(CW\*(C`NEG_FORMAT\*(C'\fR is used to determine how negative numbers
378are displayed. The result of this is that the output of this function
379my have unexpected spaces before and/or after the number. This is
380necessary so that positive and negative numbers are formatted into a
381space the same size. If you are only using positive numbers and want
382to avoid this problem, set \s-1NEG_FORMAT\s0 to \*(L"x\*(R".
383.ie n .IP "format_price($number, $precision)" 4
384.el .IP "format_price($number, \f(CW$precision\fR)" 4
385.IX Item "format_price($number, $precision)"
386Returns a string containing \f(CW$number\fR formatted similarly to
387\&\f(CW\*(C`format_number()\*(C'\fR, except that the decimal portion may have trailing
388zeroes added to make it be exactly \f(CW$precision\fR characters long, and
389the currency string will be prefixed.
390.Sp
391If the \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR attribute of the object is the empty string, no
392currency will be added.
393.Sp
394If \f(CW$precision\fR is not provided, the default of 2 will be used.
395Examples:
396.Sp
397.Vb 3
398\& format_price(12.95) yields 'USD 12.95'
399\& format_price(12) yields 'USD 12.00'
400\& format_price(12, 3) yields '12.000'
401.Ve
402.Sp
403The third example assumes that \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR is the empty string.
404.ie n .IP "format_bytes($number, $precision)" 4
405.el .IP "format_bytes($number, \f(CW$precision\fR)" 4
406.IX Item "format_bytes($number, $precision)"
407Returns a string containing \f(CW$number\fR formatted similarly to
408\&\f(CW\*(C`format_number()\*(C'\fR, except that if the number is over 1024, it will be
409divided by 1024 and \*(L"K\*(R" appended to the end; or if it is over 1048576
410(1024*1024), it will be divided by 1048576 and \*(L"M\*(R" appended to the
411end. Negative values will result in an error.
412.Sp
413If \f(CW$precision\fR is not provided, the default of 2 will be used.
414Examples:
415.Sp
416.Vb 3
417\& format_bytes(12.95) yields '12.95'
418\& format_bytes(2048) yields '2K'
419\& format_bytes(1048576) yields '1M'
420.Ve
421.IP "unformat_number($formatted)" 4
422.IX Item "unformat_number($formatted)"
423Converts a string as returned by \f(CW\*(C`format_number()\*(C'\fR,
424\&\f(CW\*(C`format_price()\*(C'\fR, or \f(CW\*(C`format_picture()\*(C'\fR, and returns the
425corresponding value as a numeric scalar. Returns \f(CW\*(C`undef\*(C'\fR if the
426number does not contain any digits. Examples:
427.Sp
428.Vb 4
429\& unformat_number('USD 12.95') yields 12.95
430\& unformat_number('USD 12.00') yields 12
431\& unformat_number('foobar') yields undef
432\& unformat_number('1234-567@.8') yields 1234567.8
433.Ve
434.Sp
435The value of \f(CW\*(C`DECIMAL_POINT\*(C'\fR is used to determine where to separate
436the integer and decimal portions of the input. All other non-digit
437characters, including but not limited to \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR and
438\&\f(CW\*(C`THOUSANDS_SEP\*(C'\fR, are removed.
439.Sp
440If the number matches the pattern of \f(CW\*(C`NEG_FORMAT\*(C'\fR \fIor\fR there is a
441``\-'' character before any of the digits, then a negative number is
442returned.
443.Sp
444If the number ends with the \f(CW\*(C`KILO_SUFFIX\*(C'\fR or \f(CW\*(C`MEGA_SUFFIX\*(C'\fR
445characters, then the number returned will be multiplied by 1024 or
4461024*1024 as appropriate.
447.SH "BUGS"
448.IX Header "BUGS"
449No known bugs at this time. Please report any problems to the author.
450.SH "AUTHOR"
451.IX Header "AUTHOR"
452William R. Ward, wrw@bayview.com
453.SH "SEE ALSO"
454.IX Header "SEE ALSO"
455\&\fIperl\fR\|(1).