5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
26 use vars qw($VERSION @ISA @EXPORT);
28 # set the version for version checking
33 C4::Format - Functions for pretty-printing strings and numbers
41 These functions return pretty-printed versions of strings and numbers.
50 @EXPORT = qw(&fmtstr &fmtdec);
54 $str = &fmtstr($env, $string, $format);
56 Returns C<$string>, padded with space to a given length.
58 C<$format> is either C<Ln> or C<Rn>, where I<n> is a positive integer.
59 C<$str> will be either left-padded or right-padded, respectively.
61 C<&fmtstr> is almost equivalent to
63 sprintf("%-n.ns", $string);
67 sprintf("%n.ns", $string);
69 The only difference is that if I<n> is less than the length of
70 C<$string>, then C<&fmtstr> will return the last I<n> characters of
71 C<$string>, whereas C<sprintf> will return the first I<n> characters.
78 # format (space pad) a string
79 # $fmt is Ln.. or Rn.. where n is the length
80 my ($env,$strg,$fmt)=@_;
81 my $align = substr($fmt,0,1);
82 my $lenst = substr($fmt,1,length($fmt)-1);
84 $strg = substr((" "x$lenst).$strg,0-$lenst,$lenst);
85 } elsif ($align eq "C" ) {
87 substr((" "x(($lenst/2)-(length($strg)/2))).$strg.(" "x$lenst),0,$lenst);
89 $strg = substr($strg.(" "x$lenst),0,$lenst);
96 $str = &fmtdec($env, $number, $format)
98 Returns a pretty-printed version of C<$number>.
100 C<$format> specifies how to print the number. It is of the form
104 where I<n> and I<m> are digits, specifying the number of digits to use
105 before and after the decimal, respectively. Thus,
107 &fmtdec(undef, 123.456, "42")
113 If I<n> is smaller than the size of the integer part, only the last
114 I<n> digits will be returned. If I<m> is greater than the number of
115 digits after the decimal in C<$number>, the result will be
116 right-padded with zeros.
118 If C<$format> has a leading dollar sign, the number is assumed to be a
119 monetary amount. C<$str> will have a dollar sign prepended to the
122 If C<$format> has a comma after the optional dollar sign, the integer
123 part will be split into three-digit groups separated by commas.
125 C<$env> is effectively ignored.
129 # FIXME - This is all terribly provincial, not at all
130 # internationalized. I'm pretty sure there's already something out
131 # there that'll figure out the current locale, look up the local
132 # currency symbol (and whether it goes on the left or right), figure
133 # out how numbers are grouped (commas, periods, or what? And how many
134 # digits per group?), and will print the whole thing prettily.
135 # But I can't find it just now. Maybe POSIX::setlocale() or
136 # perllocale(1) might help.
138 # fmtdec(undef, 12345.6, ',82') prints " 345.60"
139 # fmtdec(undef, 12345.6, '$,82') prints ".60"
143 my ($env,$numb,$fmt)=@_;
145 # FIXME - Use $fmt =~ /^(\$)?(,)?(\d)(\d)?$/ instead of this mess of
148 # See if there's a leading dollar sign.
149 my $curr = substr($fmt,0,1);
151 $fmt = substr($fmt,1,length($fmt)-1);
153 # See if there's a leading comma
154 my $comma = substr($fmt,0,1);
156 $fmt = substr($fmt,1,length($fmt)-1);
158 # See whether one number was given, or two.
160 my $left = substr($fmt,0,1);
161 if (length($fmt) == 1) {
164 $right = substr($fmt,1,1);
166 # See if $numb is a floating-point number.
171 # $numb =~ /(\d+)\.(\d+)/;
174 if (index($numb,".") == 0 ){
176 $tempdec = substr($numb,1,length($numb)-1);
178 if (index($numb,".") > 0) {
179 my $decpl = index($numb,".");
180 $tempint = substr($numb,0,$decpl);
181 $tempdec = substr($numb,$decpl+1,length($numb)-1-$decpl);
186 # If a comma was specified, then comma-separate the integer part
187 # FIXME - From the Perl Cookbook (ISBN 1-56592-243-3), sec. 2.1.7:
189 # my $test = reverse $_[0];
190 # $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
191 # return scalar reverse $text;
194 while (length($tempdec) > 3) {
195 $fnumb = ",".substr($tempint,-3,3).$fnumb;
196 substr($tempint,-3,3) = "";
198 $fnumb = substr($tempint,-3,3).$fnumb;
203 # If a dollar sign was specified, prepend a dollar sign and
204 # right-justify the number
206 $fnumb = fmtstr($env,$curr.$fnumb,"R".$left+1);
211 $fnumb = fmtstr($env,$fnumb,"R".$left);
214 # Right-pad the decimal part to the given number of digits.
216 $tempdec .= "0"x$right;
217 $tempdec = substr($tempdec,0,$right);
218 $fnumb .= ".".$tempdec;
220 return $fnumb; # FIXME - Shouldn't return a list.
230 Koha Developement team <info@koha.org>