Commit | Line | Data |
---|---|---|
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" | |
134 | Number::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" | |
161 | Perl, version 5.003 or higher. | |
162 | .PP | |
163 | \&\s-1POSIX\s0.pm will be used if present to determine locale settings. | |
164 | .PP | |
165 | Carp.pm is used for some error reporting. | |
166 | .SH "DESCRIPTION" | |
167 | .IX Header "DESCRIPTION" | |
168 | These functions provide an easy means of formatting numbers in a | |
169 | manner suitable for displaying to the user. | |
170 | .PP | |
171 | There are two ways to use this package. One is to declare an object | |
172 | of type Number::Format, which you can think of as a formatting engine. | |
173 | The various functions defined here are provided as object methods. | |
174 | The constructor \f(CW\*(C`new()\*(C'\fR can be used to set the parameters of the | |
175 | formatting 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 | |
191 | They may be specified in upper or lower case, with or without a | |
192 | leading hyphen ( \- ). | |
193 | .PP | |
194 | The 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 | |
196 | come from the \s-1POSIX\s0 locale information (see perllocale), if | |
197 | available. If your \s-1POSIX\s0 locale does not provide \f(CW\*(C`MON_THOUSANDS_SEP\*(C'\fR | |
198 | and/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 | |
201 | systems, the \s-1POSIX\s0 system is bypassed. | |
202 | .PP | |
203 | If 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 | |
205 | the 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 | |
208 | will import those variables into your namesapce and can assign values | |
209 | as if they were your own local variables. The default values for all | |
210 | the 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 | |
226 | Note however that when you first call one of the functions in this | |
227 | module \fIwithout\fR using the object-oriented interface, further setting | |
228 | of those global variables will have no effect on non-OO calls. It is | |
229 | recommended that you use the object-oriented interface instead for | |
230 | fewer headaches and a cleaner design. | |
231 | .PP | |
232 | The \f(CW\*(C`DECIMAL_FILL\*(C'\fR and \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR values are not set by the | |
233 | Locale system, but are definable by the user. They affect the output | |
234 | of \f(CW\*(C`format_number()\*(C'\fR. Setting \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR is like giving that | |
235 | value 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 | |
237 | zeroes to the right of the decimal digits until the length is the | |
238 | specified 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 | |
241 | containing the letter 'x', where that letter will be replaced by a | |
242 | positive 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 | |
244 | calling \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 | |
248 | 1024*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 | |
250 | systems. | |
251 | .PP | |
252 | The only restrictions on \f(CW\*(C`DECIMAL_POINT\*(C'\fR and \f(CW\*(C`THOUSANDS_SEP\*(C'\fR are that | |
253 | they must not be digits, must not be identical, and must each be one | |
254 | character. There are no restrictions on \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR. | |
255 | .PP | |
256 | For 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 | |
266 | Or, if you prefer not to use the object oriented interface, you can do | |
267 | this 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" | |
278 | Nothing is exported by default. To export the functions or the global | |
279 | variables defined herein, specify the function name(s) on the import | |
280 | list of the \f(CW\*(C`use Number::Format\*(C'\fR statement. To export all functions | |
281 | defined herein, use the special tag \f(CW\*(C`:subs\*(C'\fR. To export the | |
282 | variables, use the special tag \f(CW\*(C`:vars\*(C'\fR; to export both subs and vars | |
283 | you 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 )" | |
289 | Creates a new Number::Format object. Valid keys for \f(CW%args\fR are any of | |
290 | the parameters described above. Keys may be in all uppercase or all | |
291 | lowercase, and may optionally be preceded by a hyphen (\-) character. | |
292 | Example: | |
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)" | |
302 | Rounds the number to the specified precision. If \f(CW$precision\fR is | |
303 | omitted, the value of the \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR parameter is used (default | |
304 | value 2). Both input and output are numeric (the function uses math | |
305 | operators 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 | |
315 | Since this is a mathematical rather than string oriented function, | |
316 | there will be no trailing zeroes to the right of the decimal point, | |
317 | and the \f(CW\*(C`DECIMAL_POINT\*(C'\fR and \f(CW\*(C`THOUSANDS_SEP\*(C'\fR variables are ignored. | |
318 | To format your number using the \f(CW\*(C`DECIMAL_POINT\*(C'\fR and \f(CW\*(C`THOUSANDS_SEP\*(C'\fR | |
319 | variables, 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)" | |
323 | Formats a number by adding \f(CW\*(C`THOUSANDS_SEP\*(C'\fR between each set of 3 | |
324 | digits to the left of the decimal point, substituting \f(CW\*(C`DECIMAL_POINT\*(C'\fR | |
325 | for 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 | |
327 | specifier; 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 | |
329 | set, with a value that is true (not zero, undef, or the empty string). | |
330 | If \f(CW$precision\fR is omitted, the value of the \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR | |
331 | parameter (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 | |
342 | Of 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)" | |
347 | Formats a negative number. Picture should be a string that contains | |
348 | the letter \f(CW\*(C`x\*(C'\fR where the number should be inserted. For example, for | |
349 | standard negative numbers you might use ``\f(CW\*(C`\-x\*(C'\fR'', while for | |
350 | accounting purposes you might use ``\f(CW\*(C`(x)\*(C'\fR''. If the specified number | |
351 | begins with a ``\-'' character, that will be removed before formatting, | |
352 | but 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)" | |
356 | Returns a string based on \f(CW$picture\fR with the \f(CW\*(C`#\*(C'\fR characters | |
357 | replaced by digits from \f(CW$number\fR. If the length of the integer part | |
358 | of \f(CW$number\fR is too large to fit, the \f(CW\*(C`#\*(C'\fR characters are replaced with | |
359 | asterisks (\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 | |
369 | The comma (,) and period (.) you see in the picture examples should | |
370 | match the values of \f(CW\*(C`THOUSANDS_SEP\*(C'\fR and \f(CW\*(C`DECIMAL_POINT\*(C'\fR, | |
371 | respectively, for proper operation. However, the \f(CW\*(C`THOUSANDS_SEP\*(C'\fR | |
372 | characters 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 | |
374 | commas (see the first example above). There may not be more than one | |
375 | instance of \f(CW\*(C`DECIMAL_POINT\*(C'\fR in \f(CW$picture\fR. | |
376 | .Sp | |
377 | The value of \f(CW\*(C`NEG_FORMAT\*(C'\fR is used to determine how negative numbers | |
378 | are displayed. The result of this is that the output of this function | |
379 | my have unexpected spaces before and/or after the number. This is | |
380 | necessary so that positive and negative numbers are formatted into a | |
381 | space the same size. If you are only using positive numbers and want | |
382 | to 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)" | |
386 | Returns 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 | |
388 | zeroes added to make it be exactly \f(CW$precision\fR characters long, and | |
389 | the currency string will be prefixed. | |
390 | .Sp | |
391 | If the \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR attribute of the object is the empty string, no | |
392 | currency will be added. | |
393 | .Sp | |
394 | If \f(CW$precision\fR is not provided, the default of 2 will be used. | |
395 | Examples: | |
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 | |
403 | The 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)" | |
407 | Returns 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 | |
409 | divided 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 | |
411 | end. Negative values will result in an error. | |
412 | .Sp | |
413 | If \f(CW$precision\fR is not provided, the default of 2 will be used. | |
414 | Examples: | |
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)" | |
423 | Converts 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 | |
425 | corresponding value as a numeric scalar. Returns \f(CW\*(C`undef\*(C'\fR if the | |
426 | number 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 | |
435 | The value of \f(CW\*(C`DECIMAL_POINT\*(C'\fR is used to determine where to separate | |
436 | the integer and decimal portions of the input. All other non-digit | |
437 | characters, 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 | |
440 | If 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 | |
442 | returned. | |
443 | .Sp | |
444 | If the number ends with the \f(CW\*(C`KILO_SUFFIX\*(C'\fR or \f(CW\*(C`MEGA_SUFFIX\*(C'\fR | |
445 | characters, then the number returned will be multiplied by 1024 or | |
446 | 1024*1024 as appropriate. | |
447 | .SH "BUGS" | |
448 | .IX Header "BUGS" | |
449 | No known bugs at this time. Please report any problems to the author. | |
450 | .SH "AUTHOR" | |
451 | .IX Header "AUTHOR" | |
452 | William R. Ward, wrw@bayview.com | |
453 | .SH "SEE ALSO" | |
454 | .IX Header "SEE ALSO" | |
455 | \&\fIperl\fR\|(1). |