Added PODs.
[koha.git] / C4 / Format.pm
1 package C4::Format; #assumes C4/Format
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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
20
21 use strict;
22 require Exporter;
23
24
25 use vars qw($VERSION @ISA @EXPORT);
26   
27 # set the version for version checking
28 $VERSION = 0.01;
29     
30 @ISA = qw(Exporter);
31 @EXPORT = qw(&fmtstr &fmtdec);
32
33 sub fmtstr {
34   # format (space pad) a string
35   # $fmt is Ln.. or Rn.. where n is the length
36   my ($env,$strg,$fmt)=@_;
37   my $align = substr($fmt,0,1);
38   my $lenst = substr($fmt,1,length($fmt)-1);
39   if ($align eq"R" ) {
40      $strg = substr((" "x$lenst).$strg,0-$lenst,$lenst);
41   } elsif  ($align eq "C" ) {
42      $strg = 
43        substr((" "x(($lenst/2)-(length($strg)/2))).$strg.(" "x$lenst),0,$lenst);
44   } else {
45      $strg = substr($strg.(" "x$lenst),0,$lenst);
46   } 
47   return ($strg);
48 }
49
50 sub fmtdec {
51   # format a decimal
52   # $fmt is [$][,]n[m]
53   my ($env,$numb,$fmt)=@_;
54   my $curr = substr($fmt,0,1);
55   if ($curr eq "\$") {
56     $fmt = substr($fmt,1,length($fmt)-1);
57   };
58   my $comma = substr($fmt,0,1);
59   if ($comma eq ",") {
60     $fmt = substr($fmt,1,length($fmt)-1);
61   };
62   my $right;
63   my $left = substr($fmt,0,1);
64   if (length($fmt) == 1) {
65     $right = 0;
66   } else {
67     $right = substr($fmt,1,1);
68   }
69   my $fnumb = "";
70   my $tempint = "";
71   my $tempdec = "";
72   if (index($numb,".") == 0 ){
73      $tempint = 0;
74      $tempdec = substr($numb,1,length($numb)-1); 
75   } else {
76      if (index($numb,".") > 0) {
77        my $decpl = index($numb,".");
78        $tempint = substr($numb,0,$decpl);
79        $tempdec = substr($numb,$decpl+1,length($numb)-1-$decpl);
80      } else {
81        $tempint = $numb;
82        $tempdec = 0;
83      }
84      if ($comma eq ",") {
85         while (length($tempdec) > 3) {
86            $fnumb = ",".substr($tempint,-3,3).$fnumb;
87            substr($tempint,-3,3) = "";
88         }
89         $fnumb = substr($tempint,-3,3).$fnumb;
90      } else { 
91         $fnumb = $tempint; 
92      } 
93   }
94   if ($curr eq "\$") {
95      $fnumb = fmtstr($env,$curr.$fnumb,"R".$left+1);
96   } else {
97      if ($left==0) {
98         $fnumb = "";
99      } else {
100         $fnumb = fmtstr($env,$fnumb,"R".$left);
101      }
102   }   
103   if ($right > 0) {
104      $tempdec = $tempdec.("0"x$right);
105      $tempdec = substr($tempdec,0,$right);
106      $fnumb = $fnumb.".".$tempdec;
107   }
108   return ($fnumb);
109 }
110
111 END { }       # module clean-up code here (global destructor)