.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. | will give a .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' .\" expand to `' in nroff, nothing in troff, for use with C<>. .tr \(*W-|\(bv\*(Tr .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .\" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .hy 0 .if n .na .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Format 3" .TH Format 3 "2002-08-27" "perl v5.8.0" "User Contributed Perl Documentation" .SH "NAME" Number::Format \- Perl extension for formatting numbers .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 9 \& use Number::Format; \& my $x = new Number::Format %args; \& $formatted = $x->round($number, $precision); \& $formatted = $x->format_number($number, $precision, $trailing_zeroes); \& $formatted = $x->format_negative($number, $picture); \& $formatted = $x->format_picture($number, $picture); \& $formatted = $x->format_price($number, $precision); \& $formatted = $x->format_bytes($number, $precision); \& $number = $x->unformat_number($formatted); .Ve .PP .Vb 8 \& use Number::Format qw(:subs); \& $formatted = round($number, $precision); \& $formatted = format_number($number, $precision, $trailing_zeroes); \& $formatted = format_negative($number, $picture); \& $formatted = format_picture($number, $picture); \& $formatted = format_price($number, $precision); \& $formatted = format_bytes($number, $precision); \& $number = unformat_number($formatted); .Ve .SH "REQUIRES" .IX Header "REQUIRES" Perl, version 5.003 or higher. .PP \&\s-1POSIX\s0.pm will be used if present to determine locale settings. .PP Carp.pm is used for some error reporting. .SH "DESCRIPTION" .IX Header "DESCRIPTION" These functions provide an easy means of formatting numbers in a manner suitable for displaying to the user. .PP There are two ways to use this package. One is to declare an object of type Number::Format, which you can think of as a formatting engine. The various functions defined here are provided as object methods. The constructor \f(CW\*(C`new()\*(C'\fR can be used to set the parameters of the formatting engine. Valid parameters are: .PP .Vb 11 \& THOUSANDS_SEP - character inserted between groups of 3 digits \& DECIMAL_POINT - character separating integer and fractional parts \& MON_THOUSANDS_SEP - like THOUSANDS_SEP, but used for format_price \& MON_DECIMAL_POINT - like DECIMAL_POINT, but used for format_price \& INT_CURR_SYMBOL - character(s) denoting currency (see format_price()) \& DECIMAL_DIGITS - number of digits to the right of dec point (def 2) \& DECIMAL_FILL - boolean; whether to add zeroes to fill out decimal \& NEG_FORMAT - format to display negative numbers (def ``-x'') \& KILO_SUFFIX - suffix to add when format_bytes formats kilobytes \& MEGA_SUFFIX - " " " " " " megabytes \& GIGA_SUFFIX - " " " " " " gigabytes .Ve .PP They may be specified in upper or lower case, with or without a leading hyphen ( \- ). .PP The defaults for \f(CW\*(C`THOUSANDS_SEP\*(C'\fR, \f(CW\*(C`DECIMAL_POINT\*(C'\fR, \&\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 come from the \s-1POSIX\s0 locale information (see perllocale), if available. If your \s-1POSIX\s0 locale does not provide \f(CW\*(C`MON_THOUSANDS_SEP\*(C'\fR and/or \f(CW\*(C`MON_DECIMAL_POINT\*(C'\fR fields, then the \f(CW\*(C`THOUSANDS_SEP\*(C'\fR and/or \&\f(CW\*(C`DECIMAL_POINT\*(C'\fR values are used for those parameters. Some systems (e.g. Win32 ports of Perl) do not include \s-1POSIX\s0 support. In those systems, the \s-1POSIX\s0 system is bypassed. .PP If any of the above parameters are not specified when you invoke \&\f(CW\*(C`new()\*(C'\fR, then the values are taken from package global variables of the same name (e.g. \f(CW$DECIMAL_POINT\fR is the default for the \&\f(CW\*(C`DECIMAL_POINT\*(C'\fR parameter). If you use the \f(CW\*(C`:vars\*(C'\fR keyword on your \&\f(CW\*(C`use Number::Format\*(C'\fR line (see non-object-oriented example below) you will import those variables into your namesapce and can assign values as if they were your own local variables. The default values for all the parameters are: .PP .Vb 11 \& THOUSANDS_SEP = ',' \& DECIMAL_POINT = '.' \& MON_THOUSANDS_SEP = ',' \& MON_DECIMAL_POINT = '.' \& INT_CURR_SYMBOL = 'USD' \& DECIMAL_DIGITS = 2 \& DECIMAL_FILL = 0 \& NEG_FORMAT = '-x' \& KILO_SUFFIX = 'K' \& MEGA_SUFFIX = 'M' \& GIGA_SUFFIX = 'G' .Ve .PP Note however that when you first call one of the functions in this module \fIwithout\fR using the object-oriented interface, further setting of those global variables will have no effect on non-OO calls. It is recommended that you use the object-oriented interface instead for fewer headaches and a cleaner design. .PP The \f(CW\*(C`DECIMAL_FILL\*(C'\fR and \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR values are not set by the Locale system, but are definable by the user. They affect the output of \f(CW\*(C`format_number()\*(C'\fR. Setting \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR is like giving that value as the \f(CW$precision\fR argument to that function. Setting \&\f(CW\*(C`DECIMAL_FILL\*(C'\fR to a true value causes \f(CW\*(C`format_number()\*(C'\fR to append zeroes to the right of the decimal digits until the length is the specified number of digits. .PP \&\f(CW\*(C`NEG_FORMAT\*(C'\fR is only used by \f(CW\*(C`format_negative()\*(C'\fR and is a string containing the letter 'x', where that letter will be replaced by a positive representation of the number being passed to that function. \&\f(CW\*(C`format_number()\*(C'\fR and \f(CW\*(C`format_price()\*(C'\fR utilize this feature by calling \f(CW\*(C`format_negative()\*(C'\fR if the number was less than 0. .PP \&\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 \&\f(CW\*(C`format_bytes()\*(C'\fR when the value is over 1024, 1024*1024, or 1024*1024*1024, respectively. The default values are \*(L"K\*(R", \*(L"M\*(R", and \&\*(L"G\*(R". Note: we can't do \s-1TERA\s0 because of integer overflows on 32\-bit systems. .PP The only restrictions on \f(CW\*(C`DECIMAL_POINT\*(C'\fR and \f(CW\*(C`THOUSANDS_SEP\*(C'\fR are that they must not be digits, must not be identical, and must each be one character. There are no restrictions on \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR. .PP For example, a German user might include this in their code: .PP .Vb 5 \& use Number::Format; \& my $de = new Number::Format(-thousands_sep => '.', \& -decimal_point => ',', \& -int_curr_symbol => 'DEM'); \& my $formatted = $de->format_number($number); .Ve .PP Or, if you prefer not to use the object oriented interface, you can do this instead: .PP .Vb 5 \& use Number::Format qw(:subs :vars); \& $THOUSANDS_SEP = '.'; \& $DECIMAL_POINT = ','; \& $INT_CURR_SYMBOL = 'DEM'; \& my $formatted = format_number($number); .Ve .SH "EXPORTS" .IX Header "EXPORTS" Nothing is exported by default. To export the functions or the global variables defined herein, specify the function name(s) on the import list of the \f(CW\*(C`use Number::Format\*(C'\fR statement. To export all functions defined herein, use the special tag \f(CW\*(C`:subs\*(C'\fR. To export the variables, use the special tag \f(CW\*(C`:vars\*(C'\fR; to export both subs and vars you can use the tag \f(CW\*(C`:all\*(C'\fR. .SH "METHODS" .IX Header "METHODS" .ie n .IP "new( %args )" 4 .el .IP "new( \f(CW%args\fR )" 4 .IX Item "new( %args )" Creates a new Number::Format object. Valid keys for \f(CW%args\fR are any of the parameters described above. Keys may be in all uppercase or all lowercase, and may optionally be preceded by a hyphen (\-) character. Example: .Sp .Vb 3 \& my $de = new Number::Format(-thousands_sep => '.', \& -decimal_point => ',', \& -int_curr_symbol => 'DEM'); .Ve .ie n .IP "round($number, $precision)" 4 .el .IP "round($number, \f(CW$precision\fR)" 4 .IX Item "round($number, $precision)" Rounds the number to the specified precision. If \f(CW$precision\fR is omitted, the value of the \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR parameter is used (default value 2). Both input and output are numeric (the function uses math operators rather than string manipulation to do its job), The value of \&\f(CW$precision\fR may be any integer, positive or negative. Examples: .Sp .Vb 4 \& round(3.14159) yields 3.14 \& round(3.14159, 4) yields 3.1416 \& round(42.00, 4) yields 42 \& round(1234, -2) yields 1200 .Ve .Sp Since this is a mathematical rather than string oriented function, there will be no trailing zeroes to the right of the decimal point, and the \f(CW\*(C`DECIMAL_POINT\*(C'\fR and \f(CW\*(C`THOUSANDS_SEP\*(C'\fR variables are ignored. To format your number using the \f(CW\*(C`DECIMAL_POINT\*(C'\fR and \f(CW\*(C`THOUSANDS_SEP\*(C'\fR variables, use \f(CW\*(C`format_number()\*(C'\fR instead. .ie n .IP "format_number($number, $precision\fR, \f(CW$trailing_zeroes)" 4 .el .IP "format_number($number, \f(CW$precision\fR, \f(CW$trailing_zeroes\fR)" 4 .IX Item "format_number($number, $precision, $trailing_zeroes)" Formats a number by adding \f(CW\*(C`THOUSANDS_SEP\*(C'\fR between each set of 3 digits to the left of the decimal point, substituting \f(CW\*(C`DECIMAL_POINT\*(C'\fR for the decimal point, and rounding to the specified precision using \&\f(CW\*(C`round()\*(C'\fR. Note that \f(CW$precision\fR is a \fImaximum\fR precision specifier; trailing zeroes will only appear in the output if \&\f(CW$trailing_zeroes\fR is provided, or the parameter \f(CW\*(C`DECIMAL_FILL\*(C'\fR is set, with a value that is true (not zero, undef, or the empty string). If \f(CW$precision\fR is omitted, the value of the \f(CW\*(C`DECIMAL_DIGITS\*(C'\fR parameter (default value of 2) is used. Examples: .Sp .Vb 6 \& format_number(12345.6789) yields '12,345.68' \& format_number(123456.789, 2) yields '123,456.79' \& format_number(1234567.89, 2) yields '1,234,567.89' \& format_number(1234567.8, 2) yields '1,234,567.8' \& format_number(1234567.8, 2, 1) yields '1,234,567.80' \& format_number(1.23456789, 6) yields '1.234568' .Ve .Sp Of course the output would have your values of \f(CW\*(C`THOUSANDS_SEP\*(C'\fR and \&\f(CW\*(C`DECIMAL_POINT\*(C'\fR instead of ',' and '.' respectively. .ie n .IP "format_negative($number, $picture)" 4 .el .IP "format_negative($number, \f(CW$picture\fR)" 4 .IX Item "format_negative($number, $picture)" Formats a negative number. Picture should be a string that contains the letter \f(CW\*(C`x\*(C'\fR where the number should be inserted. For example, for standard negative numbers you might use ``\f(CW\*(C`\-x\*(C'\fR'', while for accounting purposes you might use ``\f(CW\*(C`(x)\*(C'\fR''. If the specified number begins with a ``\-'' character, that will be removed before formatting, but formatting will occur whether or not the number is negative. .ie n .IP "format_picture($number, $picture)" 4 .el .IP "format_picture($number, \f(CW$picture\fR)" 4 .IX Item "format_picture($number, $picture)" Returns a string based on \f(CW$picture\fR with the \f(CW\*(C`#\*(C'\fR characters replaced by digits from \f(CW$number\fR. If the length of the integer part of \f(CW$number\fR is too large to fit, the \f(CW\*(C`#\*(C'\fR characters are replaced with asterisks (\f(CW\*(C`*\*(C'\fR) instead. Examples: .Sp .Vb 5 \& format_picture(100.023, 'USD ##,###.##') yields 'USD 100.02' \& format_picture(1000.23, 'USD ##,###.##') yields 'USD 1,000.23' \& format_picture(10002.3, 'USD ##,###.##') yields 'USD 10,002.30' \& format_picture(100023, 'USD ##,###.##') yields 'USD **,***.**' \& format_picture(1.00023, 'USD #.###,###') yields 'USD 1.002,300' .Ve .Sp The comma (,) and period (.) you see in the picture examples should match the values of \f(CW\*(C`THOUSANDS_SEP\*(C'\fR and \f(CW\*(C`DECIMAL_POINT\*(C'\fR, respectively, for proper operation. However, the \f(CW\*(C`THOUSANDS_SEP\*(C'\fR characters in \f(CW$picture\fR need not occur every three digits; the \&\fIonly\fR use of that variable by this function is to remove leading commas (see the first example above). There may not be more than one instance of \f(CW\*(C`DECIMAL_POINT\*(C'\fR in \f(CW$picture\fR. .Sp The value of \f(CW\*(C`NEG_FORMAT\*(C'\fR is used to determine how negative numbers are displayed. The result of this is that the output of this function my have unexpected spaces before and/or after the number. This is necessary so that positive and negative numbers are formatted into a space the same size. If you are only using positive numbers and want to avoid this problem, set \s-1NEG_FORMAT\s0 to \*(L"x\*(R". .ie n .IP "format_price($number, $precision)" 4 .el .IP "format_price($number, \f(CW$precision\fR)" 4 .IX Item "format_price($number, $precision)" Returns a string containing \f(CW$number\fR formatted similarly to \&\f(CW\*(C`format_number()\*(C'\fR, except that the decimal portion may have trailing zeroes added to make it be exactly \f(CW$precision\fR characters long, and the currency string will be prefixed. .Sp If the \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR attribute of the object is the empty string, no currency will be added. .Sp If \f(CW$precision\fR is not provided, the default of 2 will be used. Examples: .Sp .Vb 3 \& format_price(12.95) yields 'USD 12.95' \& format_price(12) yields 'USD 12.00' \& format_price(12, 3) yields '12.000' .Ve .Sp The third example assumes that \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR is the empty string. .ie n .IP "format_bytes($number, $precision)" 4 .el .IP "format_bytes($number, \f(CW$precision\fR)" 4 .IX Item "format_bytes($number, $precision)" Returns a string containing \f(CW$number\fR formatted similarly to \&\f(CW\*(C`format_number()\*(C'\fR, except that if the number is over 1024, it will be divided by 1024 and \*(L"K\*(R" appended to the end; or if it is over 1048576 (1024*1024), it will be divided by 1048576 and \*(L"M\*(R" appended to the end. Negative values will result in an error. .Sp If \f(CW$precision\fR is not provided, the default of 2 will be used. Examples: .Sp .Vb 3 \& format_bytes(12.95) yields '12.95' \& format_bytes(2048) yields '2K' \& format_bytes(1048576) yields '1M' .Ve .IP "unformat_number($formatted)" 4 .IX Item "unformat_number($formatted)" Converts a string as returned by \f(CW\*(C`format_number()\*(C'\fR, \&\f(CW\*(C`format_price()\*(C'\fR, or \f(CW\*(C`format_picture()\*(C'\fR, and returns the corresponding value as a numeric scalar. Returns \f(CW\*(C`undef\*(C'\fR if the number does not contain any digits. Examples: .Sp .Vb 4 \& unformat_number('USD 12.95') yields 12.95 \& unformat_number('USD 12.00') yields 12 \& unformat_number('foobar') yields undef \& unformat_number('1234-567@.8') yields 1234567.8 .Ve .Sp The value of \f(CW\*(C`DECIMAL_POINT\*(C'\fR is used to determine where to separate the integer and decimal portions of the input. All other non-digit characters, including but not limited to \f(CW\*(C`INT_CURR_SYMBOL\*(C'\fR and \&\f(CW\*(C`THOUSANDS_SEP\*(C'\fR, are removed. .Sp If the number matches the pattern of \f(CW\*(C`NEG_FORMAT\*(C'\fR \fIor\fR there is a ``\-'' character before any of the digits, then a negative number is returned. .Sp If the number ends with the \f(CW\*(C`KILO_SUFFIX\*(C'\fR or \f(CW\*(C`MEGA_SUFFIX\*(C'\fR characters, then the number returned will be multiplied by 1024 or 1024*1024 as appropriate. .SH "BUGS" .IX Header "BUGS" No known bugs at this time. Please report any problems to the author. .SH "AUTHOR" .IX Header "AUTHOR" William R. Ward, wrw@bayview.com .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fIperl\fR\|(1).