1 package C4::Format; #assumes C4/Format
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
25 use vars qw($VERSION @ISA @EXPORT);
27 # set the version for version checking
32 C4::Format - Functions for pretty-printing strings and numbers
40 These functions return pretty-printed versions of strings and numbers.
49 @EXPORT = qw(&fmtstr &fmtdec);
53 $str = &fmtstr($env, $string, $format);
55 Returns C<$string>, padded with space to a given length.
57 C<$format> is either C<Ln> or C<Rn>, where I<n> is a positive integer.
58 C<$str> will be either left-padded or right-padded, respectively.
60 C<&fmtstr> is almost equivalent to
62 sprintf("%-n.ns", $string);
66 sprintf("%n.ns", $string);
68 The only difference is that if I<n> is less than the length of
69 C<$string>, then C<&fmtstr> will return the last I<n> characters of
70 C<$string>, whereas C<sprintf> will return the first I<n> characters.
77 # format (space pad) a string
78 # $fmt is Ln.. or Rn.. where n is the length
79 my ($env,$strg,$fmt)=@_;
80 my $align = substr($fmt,0,1);
81 my $lenst = substr($fmt,1,length($fmt)-1);
83 $strg = substr((" "x$lenst).$strg,0-$lenst,$lenst);
84 } elsif ($align eq "C" ) {
86 substr((" "x(($lenst/2)-(length($strg)/2))).$strg.(" "x$lenst),0,$lenst);
88 $strg = substr($strg.(" "x$lenst),0,$lenst);
95 $str = &fmtdec($env, $number, $format)
97 Returns a pretty-printed version of C<$number>.
99 C<$format> specifies how to print the number. It is of the form
103 where I<n> and I<m> are digits, specifying the number of digits to use
104 before and after the decimal, respectively. Thus,
106 &fmtdec(undef, 123.456, "42")
112 If I<n> is smaller than the size of the integer part, only the last
113 I<n> digits will be returned. If I<m> is greater than the number of
114 digits after the decimal in C<$number>, the result will be
115 right-padded with zeros.
117 If C<$format> has a leading dollar sign, the number is assumed to be a
118 monetary amount. C<$str> will have a dollar sign prepended to the
121 If C<$format> has a comma after the optional dollar sign, the integer
122 part will be split into three-digit groups separated by commas.
126 # FIXME - This is all terribly provincial, not at all
127 # internationalized. I'm pretty sure there's already something out
128 # there that'll figure out the current locale, look up the local
129 # currency symbol (and whether it goes on the left or right), figure
130 # out how numbers are grouped (commas, periods, or what? And how many
131 # digits per group?), and will print the whole thing prettily.
132 # But I can't find it just now. Maybe POSIX::setlocale() or
133 # perllocale(1) might help.
135 # fmtdec(undef, 12345.6, ',82') prints " 345.60"
136 # fmtdec(undef, 12345.6, '$,82') prints ".60"
140 my ($env,$numb,$fmt)=@_;
142 # FIXME - Use $fmt =~ /^(\$)?(,)?(\d)(\d)?$/ instead of this mess of
145 # See if there's a leading dollar sign.
146 my $curr = substr($fmt,0,1);
148 $fmt = substr($fmt,1,length($fmt)-1);
150 # See if there's a leading comma
151 my $comma = substr($fmt,0,1);
153 $fmt = substr($fmt,1,length($fmt)-1);
155 # See whether one number was given, or two.
157 my $left = substr($fmt,0,1);
158 if (length($fmt) == 1) {
161 $right = substr($fmt,1,1);
163 # See if $numb is a floating-point number.
168 # $numb =~ /(\d+)\.(\d+)/;
171 if (index($numb,".") == 0 ){
173 $tempdec = substr($numb,1,length($numb)-1);
175 if (index($numb,".") > 0) {
176 my $decpl = index($numb,".");
177 $tempint = substr($numb,0,$decpl);
178 $tempdec = substr($numb,$decpl+1,length($numb)-1-$decpl);
183 # If a comma was specified, then comma-separate the integer part
185 while (length($tempdec) > 3) {
186 $fnumb = ",".substr($tempint,-3,3).$fnumb;
187 substr($tempint,-3,3) = "";
189 $fnumb = substr($tempint,-3,3).$fnumb;
194 # If a dollar sign was specified, prepend a dollar sign and
195 # right-justify the number
197 $fnumb = fmtstr($env,$curr.$fnumb,"R".$left+1);
202 $fnumb = fmtstr($env,$fnumb,"R".$left);
205 # Right-pad the decimal part to the given number of digits.
207 $tempdec = $tempdec.("0"x$right);
208 $tempdec = substr($tempdec,0,$right);
209 $fnumb = $fnumb.".".$tempdec;
211 return ($fnumb); # FIXME - Shouldn't return a list.
214 END { } # module clean-up code here (global destructor)
223 Koha Developement team <info@koha.org>