File Coverage

blib/lib/Lingua/PL/Numbers.pm
Criterion Covered Total %
statement 9 180 5.0
branch 0 126 0.0
condition 0 47 0.0
subroutine 3 18 16.6
pod 0 15 0.0
total 12 386 3.1


line stmt bran cond sub pod time code
1             package Lingua::PL::Numbers;
2              
3             $VERSION = '1.0';
4              
5 1     1   8147 use strict;
  1         3  
  1         46  
6 1     1   6 use vars qw($Idziesiatka);
  1         2  
  1         50  
7 1     1   6 use Carp;
  1         16  
  1         4760  
8              
9             sub new
10             {
11 0     0 0   my $class = shift;
12 0   0       my $number = shift || '';
13 0           $Idziesiatka=0;
14              
15 0           my $self = {};
16 0           bless $self, $class;
17              
18 0 0         if( $number =~ /\d+/ ) {
19 0           return( $self->parse($number) );
20             };
21              
22 0           return( $self );
23             };
24              
25              
26             sub parse
27             {
28 0     0 0   my $self = shift;
29 0           my $number = shift;
30 0           return( SLOWNIE($number,0) );
31             };
32              
33             sub currency
34             {
35 0     0 0   my $self = shift;
36 0           my $number = shift;
37 0           return( SLOWNIE($number,1) );
38             };
39              
40              
41              
42             sub SLOWNIE {
43 0     0 0   my ($Numer,$currency)=@_;
44              
45 0           my ($temps, $tempd, $tempj, $zlote, $grosze, $Licznik, $grd, $grj, $MiejsceDz, $T_S, $SLOWNIE);
46              
47 0 0         if ($Numer == 0) {
48 0 0         if ($currency) {
49 0           $SLOWNIE = "zero zł zero gr";
50             } else {
51 0           $SLOWNIE = "zero";
52             }
53             } else {
54 0 0 0       if ($Numer > 9999999999999.99 || $Numer < 0) {
55 0           carp "out of range in $Numer";
56 0           $SLOWNIE = "out of range";
57             } else {
58 0           $Numer = Trim($Numer);
59 0           $MiejsceDz = InStr($Numer);
60 0 0 0       if ($MiejsceDz > 0 && Right($Numer,2) ne "00") {
    0          
61 0 0         if ($currency) {
62 0           $grosze = Left(Mid($Numer, $MiejsceDz + 1)."00", 2);
63 0           $grd = Dziesiatki(Right($grosze, 2));
64 0 0         if ($Idziesiatka!=1) {
65 0           $grj = Jednostki(Right($grosze, 1));
66             }
67 0           $grosze = " ".$grd.$grj."gr";
68 0           $Numer = Trim(Left($Numer, $MiejsceDz - 1));
69             } else {
70 0           carp "no decimals allowed in parse mode in $Numer";
71 0           $zlote = "no decimals allowed in parse mode in $Numer";
72             }
73             } elsif ($currency) {
74 0           $grosze = " zero gr";
75             }
76 0 0 0       if ($Numer>0 && ($currency || !$MiejsceDz)) {
    0 0        
      0        
77 0           $Licznik = 1;
78 0           while ($Numer ne "") {
79 0           $temps = "";
80 0           $tempd = "";
81 0           $tempj = "";
82 0           $temps = Setki(Right("000".$Numer, 3));
83 0           $tempd = Dziesiatki(Right("00".$Numer, 2));
84 0 0         if ($Idziesiatka!=1) {
85 0           $tempj = Jednostki(Right($Numer, 1));
86             }
87 0 0 0       if ($Licznik==1) {
    0 0        
    0          
88 0           $T_S = $temps.$tempd.$tempj;
89             } elsif ($Licznik==2) {
90 0           $T_S = $temps.$tempd.$tempj.KTys($Numer);
91             } elsif ($Licznik==3||$Licznik==4||$Licznik==5) {
92 0           $T_S = $temps.$tempd.$tempj.KMil($Numer, $Licznik);
93             }
94 0           $zlote = $T_S.$zlote;
95            
96 0 0         if (length($Numer) > 3) {
97 0           $Numer = Left($Numer, length($Numer) - 3);
98 0           $Licznik++;
99             } else {
100 0           $Numer = "";
101             }
102             }
103             } elsif ($currency || !$MiejsceDz) {
104 0           $zlote = "zero "
105             }
106 0 0         if ($Numer > -1) {
107 0 0         if ($currency) {
108 0           $SLOWNIE = $zlote."zł".$grosze;
109             } else {
110 0           $SLOWNIE = $zlote;
111             }
112             }
113             }
114             }
115 0           return $SLOWNIE;
116             }
117              
118             sub KTys {
119 0     0 0   my $Numer=shift;
120 0           my $KTys;
121 0           my $tys=Val(Right("000".$Numer, 3));
122 0 0         if ($tys == 0) {
123 0           $KTys = "";
124             } else {
125 0           $tys = Val(Right($Numer, 2));
126 0 0         if ($tys == 1) {
127 0           $KTys = "ąc ";
128             } else {
129 0 0 0       if ($tys == 12 || $tys == 13 || $tys == 14) {
      0        
130 0           $KTys = "ęcy ";
131             } else {
132 0           $tys = Val(Right($Numer, 1));
133             }
134 0 0 0       if ( $tys == 2 || $tys == 3 || $tys == 4) {
      0        
135 0           $KTys = "ące ";
136             } else {
137 0           $KTys = "ęcy ";
138             }
139             }
140 0           $KTys = "tysi".$KTys;
141             }
142 0           return $KTys;
143             }
144              
145             sub KMil {
146 0     0 0   my ($Numer, $L)=@_;
147 0           my ($KMil,$mil);
148 0           my @RzadW;
149 0           $RzadW[3] = "milion";
150 0           $RzadW[4] = "miliard";
151 0           $RzadW[5] = "bilion";
152 0           $mil = Val(Right("000".$Numer, 3));
153 0 0         if ($mil == 0) {
154 0           $KMil = "";
155             } else {
156 0           $mil = Val(Right($Numer, 2));
157 0 0         if ($mil == 1) {
158 0           $KMil = " ";
159             } else {
160 0 0 0       if ($mil == 12 || $mil == 13 || $mil == 14) {
      0        
161 0           $KMil = "ów ";
162             } else {
163 0           $mil = Val(Right($Numer, 1));
164 0 0 0       if ($mil == 2 || $mil == 3 || $mil == 4) {
      0        
165 0           $KMil = "y ";
166             } else {
167 0           $KMil = "ów ";
168             }
169             }
170             }
171 0           $KMil = $RzadW[$L].$KMil;
172             }
173 0           return $KMil;
174             }
175              
176             sub Setki {
177 0     0 0   my $Numer=shift;
178 0           my ($setka, $wynik);
179 0           $setka = Val(Left($Numer, 1));
180 0 0         if ($setka == 1) {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
181 0           $wynik= "sto ";
182             } elsif ($setka == 2) {
183 0           $wynik = "dwieście ";
184             } elsif ($setka == 3) {
185 0           $wynik = "trzysta ";
186             } elsif ($setka == 4) {
187 0           $wynik = "czterysta ";
188             } elsif ($setka == 5) {
189 0           $wynik = "pięćset ";
190             } elsif ($setka == 6) {
191 0           $wynik = "sześćset ";
192             } elsif ($setka == 7) {
193 0           $wynik = "siedemset ";
194             } elsif ($setka == 8) {
195 0           $wynik = "osiemset ";
196             } elsif ($setka == 9) {
197 0           $wynik = "dziewięćset ";
198             } else {
199 0           $wynik = "";
200             }
201 0           return $wynik;
202             }
203              
204             sub Dziesiatki {
205 0     0 0   my $Number=shift;
206 0           my $wynik="";
207 0           $Idziesiatka = Val(Left($Number, 1));
208 0 0         if ($Idziesiatka == 1) {
209 0 0         if (Val($Number) == 10) {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
210 0           $wynik = "dziesięć ";
211             } elsif (Val($Number) == 11) {
212 0           $wynik = "jedenaście ";
213             } elsif (Val($Number) == 12) {
214 0           $wynik = "dwanaście ";
215             } elsif (Val($Number) == 13) {
216 0           $wynik = "trzynaście ";
217             } elsif (Val($Number) == 14) {
218 0           $wynik = "czternaście ";
219             } elsif (Val($Number) == 15) {
220 0           $wynik = "piętnaście ";
221             } elsif (Val($Number) == 16) {
222 0           $wynik = "szesnaście ";
223             } elsif (Val($Number) == 17) {
224 0           $wynik = "siedemnaście ";
225             } elsif (Val($Number) == 18) {
226 0           $wynik = "osiemnaście ";
227             } elsif (Val($Number) == 19) {
228 0           $wynik = "dziewiętnaście ";
229             }
230             } else {
231 0 0         if ($Idziesiatka == 2) {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
232 0           $wynik = "dwadzieścia ";
233             } elsif ($Idziesiatka == 3) {
234 0           $wynik = "trzydzieści ";
235             } elsif ($Idziesiatka == 4) {
236 0           $wynik = "czterdzieści ";
237             } elsif ($Idziesiatka == 5) {
238 0           $wynik = "pięćdziesiąt ";
239             } elsif ($Idziesiatka == 6) {
240 0           $wynik = "sześćdziesiąt ";
241             } elsif ($Idziesiatka == 7) {
242 0           $wynik = "siedemdziesiąt ";
243             } elsif ($Idziesiatka == 8) {
244 0           $wynik = "osiemdziesiąt ";
245             } elsif ($Idziesiatka == 9) {
246 0           $wynik = "dziewięćdziesiąt ";
247             }
248             }
249 0           return $wynik;
250             }
251            
252             sub Jednostki {
253 0     0 0   my $Numer=shift;
254 0           my ($jedst, $wynik);
255 0           $jedst = Val(Right($Numer, 1));
256 0 0         if ($jedst == 1) {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
257 0           $wynik = "jeden ";
258             } elsif ($jedst == 2) {
259 0           $wynik = "dwa ";
260             } elsif ($jedst == 3) {
261 0           $wynik = "trzy ";
262             } elsif ($jedst == 4) {
263 0           $wynik = "cztery ";
264             } elsif ($jedst == 5) {
265 0           $wynik = "pięć ";
266             } elsif ($jedst == 6) {
267 0           $wynik = "sześć ";
268             } elsif ($jedst == 7) {
269 0           $wynik = "siedem ";
270             } elsif ($jedst == 8) {
271 0           $wynik = "osiem ";
272             } elsif ($jedst == 9) {
273 0           $wynik = "dziewięć ";
274             }
275 0           return $wynik;
276             }
277              
278             sub Left {
279 0     0 0   my ($Numer, $count) = @_;
280 0           $Numer = substr($Numer,0,$count);
281 0           return $Numer;
282             }
283              
284             sub Right {
285 0     0 0   my ($Numer, $count) = @_;
286 0           $Numer = substr($Numer,-$count);
287 0           return $Numer;
288             }
289              
290             sub Trim {
291 0     0 0   my $Numer = shift;
292 0           $Numer=~s/^\s+//;
293 0           $Numer=~s/\s+$//;
294 0           return $Numer;
295             }
296              
297             sub Val {
298 0     0 0   my $Numer = shift;
299 0           $Numer=~s/\D//g;
300 0           return $Numer;
301             }
302              
303             sub Mid {
304 0     0 0   my ($Numer, $count) = @_;
305 0           $Numer = substr($Numer,$count-1);
306             }
307              
308             sub InStr {
309 0     0 0   my $Numer = shift;
310 0           my $ret=0;
311 0 0         if ($Numer=~/^(\d+)\./) {
312 0           $ret=length($1)+1;
313             }
314 0           return $ret;
315             }
316              
317              
318              
319             1;
320              
321             =pod
322              
323             =head1 NAME
324              
325             Lingua::PL::Numbers - Perl module for converting numeric values into their Polish equivalents
326              
327            
328             =head1 DESCRIPTION
329              
330             Initial release, documentation and updates will follow.
331              
332             =head1 SYNOPSIS
333              
334             use Lingua::PL::Numbers;
335            
336             my $numbers = Lingua::PL::Numbers->new;
337              
338              
339             my $text = $numbers->parse( 123 );
340              
341             # prints 'sto dwadzieścia trzy'
342             print $text;
343              
344              
345             my $currency = $numbers->currency ( 123.45 );
346              
347             # prints 'sto dwadzieścia trzy zł czterdzieści pięć gr'
348             print $currency;
349              
350              
351             =head1 KNOWN BUGS
352              
353             None, but that does not mean there are not any.
354              
355             =head1 AUTHOR
356              
357             Henrik Steffen,
358              
359             =cut
360