blib/lib/Lingua/POL/Num2Word.pm untested

filter: Statementstmt 0.0% 0 / 239 Branchbran 0.0% 0 / 150 Conditioncond 0.0% 0 / 69 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 501 CC 76 SCAR 52.2
CC
76
Cov
0%
Dziesiatki
420.0
SLOWNIE
306.0
num2pol_ordinal
182.0
CRAP 185.2
1 # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8 -*-
2  
3 package Lingua::POL::Num2Word;
4 # ABSTRACT: Number 2 word conversion in POL.
5  
6 use 5.16.0;
7 use utf8;
8 use warnings;
9  
10 # {{{ use block
11  
12 use vars qw($Idziesiatka);
13  
14 use lib $ENV{PMLIB_INC};
15  
16 use Carp;
17 use Export::Attrs;
18  
19 # }}}
20 # {{{ var block
21 our $VERSION = '0.2603270';
22  
23 # }}}
24  
25 # {{{ new
26  
27 sub new {
28 my $class = shift;
29 my $number = shift || '';
30 $Idziesiatka=0;
31  
32 my $self = {};
33 bless $self, $class;
34  
35 if( $number =~ /\d+/ ) {
36 return( $self->parse($number) );
37 }
38  
39 return( $self );
40 }
41  
42 # }}}
43 # {{{ parse
44  
45 sub num2pol_cardinal :Export {
46 my $number = shift;
47 my $obj = Lingua::POL::Num2Word->new();
48 return $obj->parse($number);
49 }
50  
51 sub parse :Export {
52 my $self = shift;
53 my $number = shift;
54  
55 return( SLOWNIE($number,0) );
56 }
57  
58 # }}}
59 # {{{ currency
60  
61 sub currency {
62 my $self = shift;
63 my $number = shift;
64  
65 return( SLOWNIE($number,1) );
66 }
67  
68 # }}}
69 # {{{ SLOWNIE
70  
71 sub SLOWNIE {
72 my $Numer = shift // 0;
73 my $currency = shift;
74  
75 my ($temps, $tempd, $tempj, $zlote, $grosze, $Licznik, $grd, $grj, $MiejsceDz, $T_S, $SLOWNIE);
76  
77 if ($Numer == 0) {
78 if ($currency) {
79 $SLOWNIE = "zero zl zero gr";
80 } else {
81 $SLOWNIE = "zero";
82 }
83 }
84 else {
85 if ($Numer > 9999999999999.99 || $Numer < 0) {
86 #carp "out of range in $Numer";
87 $SLOWNIE = "out of range";
88 }
89 else {
90 $Numer = Trim($Numer);
91 $MiejsceDz = InStr($Numer);
92 if ($MiejsceDz > 0 && Right($Numer,2) ne "00") {
93 if ($currency) {
94 $grosze = Left(Mid($Numer, $MiejsceDz + 1)."00", 2);
95 $grd = Dziesiatki(Right($grosze, 2));
96 if ($Idziesiatka!=1) {
97 $grj = Jednostki(Right($grosze, 1));
98 }
99 $grosze = " ".$grd.$grj."gr";
100 $Numer = Trim(Left($Numer, $MiejsceDz - 1));
101 }
102 else {
103 carp "no decimals allowed in parse mode in $Numer";
104 $zlote = "no decimals allowed in parse mode in $Numer";
105 }
106 }
107 elsif ($currency) {
108 $grosze = " zero gr";
109 }
110 if ($Numer>0 && ($currency || !$MiejsceDz)) {
111 $Licznik = 1;
112 while ($Numer ne "") {
113 $tempj = "";
114 $temps = Setki(Right("000".$Numer, 3)) // '';
115 $tempd = Dziesiatki(Right("00".$Numer, 2)) // '';
116 if ($Idziesiatka!=1) {
117 $tempj = Jednostki(Right($Numer, 1)) // '';
118 }
119 if ($Licznik==1) {
120 $T_S = $temps.$tempd.$tempj;
121 }
122 elsif ($Licznik==2) {
123 $T_S = $temps.$tempd.$tempj.KTys($Numer);
124 } elsif ($Licznik==3||$Licznik==4||$Licznik==5) {
125 $T_S = $temps.$tempd.$tempj.KMil($Numer, $Licznik);
126 }
127 $zlote = $T_S.($zlote // '');
128  
129 if (length($Numer) > 3) {
130 $Numer = Left($Numer, length($Numer) - 3);
131 $Licznik++;
132 }
133 else {
134 $Numer = "";
135 }
136 }
137 } elsif ($currency || !$MiejsceDz) {
138 $zlote = "zero "
139 }
140 if ($Numer !~ /^\d+$/ or $Numer > -1) {
141 if ($currency) {
142 $SLOWNIE = $zlote."zl".$grosze;
143 } else {
144 $SLOWNIE = $zlote;
145 }
146 }
147 }
148 }
149  
150 return $SLOWNIE;
151 }
152  
153 # }}}
154 # {{{ KTys
155  
156 sub KTys {
157 my $Numer = shift;
158 my $KTys;
159 my $tys=Val(Right("000".$Numer, 3));
160  
161 if ($tys == 0) {
162 $KTys = "";
163 }
164 else {
165 $tys = Val(Right($Numer, 2));
166 if ($tys == 1) {
167 $KTys = "ąc ";
168 }
169 else {
170 if ($tys == 12 || $tys == 13 || $tys == 14) {
171 $KTys = "ęcy ";
172 }
173 else {
174 $tys = Val(Right($Numer, 1));
175 }
176 if ( $tys == 2 || $tys == 3 || $tys == 4) {
177 $KTys = "ące ";
178 }
179 else {
180 $KTys = "ęcy ";
181 }
182 }
183 $KTys = "tysi".$KTys;
184 }
185  
186 return $KTys;
187 }
188  
189 # }}}
190 # {{{ KMil
191  
192 sub KMil {
193 my ($Numer, $L)=@_;
194 my ($KMil,$mil);
195 my @RzadW;
196 $RzadW[3] = "milion";
197 $RzadW[4] = "miliard";
198 $RzadW[5] = "bilion";
199 $mil = Val(Right("000".$Numer, 3));
200 if ($mil == 0) {
201 $KMil = "";
202 }
203 else {
204 $mil = Val(Right($Numer, 2));
205 if ($mil == 1) {
206 $KMil = " ";
207 }
208 else {
209 if ($mil == 12 || $mil == 13 || $mil == 14) {
210 $KMil = "ów ";
211 }
212 else {
213 $mil = Val(Right($Numer, 1));
214 if ($mil == 2 || $mil == 3 || $mil == 4) {
215 $KMil = "y ";
216 } else {
217 $KMil = "ów ";
218 }
219 }
220 }
221 $KMil = $RzadW[$L].$KMil;
222 }
223  
224 return $KMil;
225 }
226  
227 # }}}
228 # {{{ Setki
229  
230 sub Setki {
231 my $Numer=shift;
232 my ($setka, $wynik);
233 $setka = Val(Left($Numer, 1));
234 if ($setka == 1) {
235 $wynik= "sto ";
236 } elsif ($setka == 2) {
237 $wynik = 'dwieście ';
238 } elsif ($setka == 3) {
239 $wynik = 'trzysta ';
240 } elsif ($setka == 4) {
241 $wynik = 'czterysta ';
242 } elsif ($setka == 5) {
243 $wynik = 'pięćset ';
244 } elsif ($setka == 6) {
245 $wynik = 'sześćset ';
246 } elsif ($setka == 7) {
247 $wynik = 'siedemset ';
248 } elsif ($setka == 8) {
249 $wynik = 'osiemset ';
250 } elsif ($setka == 9) {
251 $wynik = 'dziewięćset ';
252 } else {
253 $wynik = '';
254 }
255  
256 return $wynik;
257 }
258  
259 # }}}
260 # {{{ Dziesiatki
261  
262 sub Dziesiatki {
263 my $Number = shift;
264 my $wynik = '';
265  
266 $Idziesiatka = Val(Left($Number, 1));
267 if ($Idziesiatka == 1) {
268 my $valnum = Val($Number);
269 if ($valnum == 10) { $wynik = 'dziesięć '; }
270 elsif ($valnum == 11) { $wynik = 'jedenaście '; }
271 elsif ($valnum == 12) { $wynik = 'dwanaście '; }
272 elsif ($valnum == 13) { $wynik = 'trzynaście '; }
273 elsif ($valnum == 14) { $wynik = 'czternaście '; }
274 elsif ($valnum == 15) { $wynik = 'piętnaście '; }
275 elsif ($valnum == 16) { $wynik = 'szesnaście '; }
276 elsif ($valnum == 17) { $wynik = 'siedemnaście '; }
277 elsif ($valnum == 18) { $wynik = 'osiemnaście '; }
278 elsif ($valnum == 19) { $wynik = 'dziewiętnaście '; }
279 }
280 else {
281 if ($Idziesiatka == 2) { $wynik = 'dwadzieścia '; }
282 if ($Idziesiatka == 3) { $wynik = 'trzydzieści '; }
283 if ($Idziesiatka == 4) { $wynik = 'czterdzieści '; }
284 if ($Idziesiatka == 5) { $wynik = 'pięćdziesiąt '; }
285 if ($Idziesiatka == 6) { $wynik = 'sześćdziesiąt '; }
286 if ($Idziesiatka == 7) { $wynik = 'siedemdziesiąt '; }
287 if ($Idziesiatka == 8) { $wynik = 'osiemdziesiąt '; }
288 if ($Idziesiatka == 9) { $wynik = 'dziewięćdziesiąt '; }
289 }
290  
291 return $wynik;
292 }
293  
294 # }}}
295 # {{{ Jednostki
296  
297 sub Jednostki {
298 my $Numer=shift;
299 my ($jedst, $wynik);
300 $jedst = Val(Right($Numer, 1));
301 if ($jedst == 1) {
302 $wynik = "jeden ";
303 } elsif ($jedst == 2) {
304 $wynik = "dwa ";
305 } elsif ($jedst == 3) {
306 $wynik = "trzy ";
307 } elsif ($jedst == 4) {
308 $wynik = "cztery ";
309 } elsif ($jedst == 5) {
310 $wynik = "pięć ";
311 } elsif ($jedst == 6) {
312 $wynik = "sześć ";
313 } elsif ($jedst == 7) {
314 $wynik = "siedem ";
315 } elsif ($jedst == 8) {
316 $wynik = "osiem ";
317 } elsif ($jedst == 9) {
318 $wynik = "dziewięć ";
319 }
320 return $wynik;
321 }
322  
323 # }}}
324 # {{{ Left
325  
326 sub Left {
327 my ($Numer, $count) = @_;
328 $Numer = substr($Numer,0,$count);
329  
330 return $Numer;
331 }
332  
333 # }}}
334 # {{{ Right
335  
336 sub Right {
337 my ($Numer, $count) = @_;
338 $Numer = substr($Numer,-$count);
339  
340 return $Numer;
341 }
342  
343 # }}}
344 # {{{ Trim
345  
346 sub Trim {
347 my $Numer = shift;
348 $Numer=~s/^\s+//;
349 $Numer=~s/\s+$//;
350  
351 return $Numer;
352 }
353  
354 # }}}
355 # {{{ Val
356  
357 sub Val {
358 my $Numer = shift;
359  
360 $Numer=~s/\D//g;
361  
362 return $Numer;
363 }
364  
365 # }}}
366 # {{{ Mid
367  
368 sub Mid {
369 my ($Numer, $count) = @_;
370  
371 return ($Numer = substr($Numer,$count-1));
372 }
373  
374 # }}}
375 # {{{ InStr
376  
377 sub InStr {
378 my $Numer = shift;
379 my $ret=0;
380 if ($Numer=~/^(\d+)\./) {
381 $ret=length($1)+1;
382 }
383  
384 return $ret;
385 }
386  
387 # }}}
388  
389  
390 # {{{ num2pol_ordinal number to ordinal string conversion
391  
392 sub num2pol_ordinal :Export {
393 my $number = shift;
394  
395 croak 'You should specify a number from interval [0, 999_999_999]'
396 if !defined $number
397 || $number !~ m{\A\d+\z}xms
398 || $number < 0
399 || $number > 999_999_999;
400  
401 # Irregular ordinals 0-19
402 my %base = (
403 0 => 'zerowy',
404 1 => 'pierwszy',
405 2 => 'drugi',
406 3 => 'trzeci',
407 4 => 'czwarty',
408 5 => 'piąty',
409 6 => 'szósty',
410 7 => 'siódmy',
411 8 => 'ósmy',
412 9 => 'dziewiąty',
413 10 => 'dziesiąty',
414 11 => 'jedenasty',
415 12 => 'dwunasty',
416 13 => 'trzynasty',
417 14 => 'czternasty',
418 15 => 'piętnasty',
419 16 => 'szesnasty',
420 17 => 'siedemnasty',
421 18 => 'osiemnasty',
422 19 => 'dziewiętnasty',
423 );
424  
425 return $base{$number} if exists $base{$number};
426  
427 # Tens ordinals
428 my %tens_ord = (
429 20 => 'dwudziesty',
430 30 => 'trzydziesty',
431 40 => 'czterdziesty',
432 50 => 'pięćdziesiąty',
433 60 => 'sześćdziesiąty',
434 70 => 'siedemdziesiąty',
435 80 => 'osiemdziesiąty',
436 90 => 'dziewięćdziesiąty',
437 );
438  
439 # Hundreds ordinals
440 my %hundreds_ord = (
441 100 => 'setny',
442 200 => 'dwusetny',
443 300 => 'trzechsetny',
444 400 => 'czterechsetny',
445 500 => 'pięćsetny',
446 600 => 'sześćsetny',
447 700 => 'siedemsetny',
448 800 => 'osiemsetny',
449 900 => 'dziewięćsetny',
450 );
451  
452 # For compound numbers, Polish uses: ordinal of each significant part
453 # 21 = "dwudziesty pierwszy", 100 = "setny", 125 = "sto dwudziesty piąty"
454 # For large numbers: cardinal prefix + ordinal of last significant part
455  
456 if ($number >= 1_000_000) {
457 my $millions = int($number / 1_000_000);
458 my $remainder = $number % 1_000_000;
459 if ($remainder == 0) {
460 return 'milionowy' if $millions == 1;
461 return SLOWNIE($millions, 0) . 'milionowy';
462 }
463 my $prefix = SLOWNIE($millions * 1_000_000, 0);
464 return $prefix . num2pol_ordinal($remainder);
465 }
466  
467 if ($number >= 1_000) {
468 my $thousands = int($number / 1_000);
469 my $remainder = $number % 1_000;
470 if ($remainder == 0) {
471 return 'tysięczny' if $thousands == 1;
472 return SLOWNIE($thousands, 0) . 'tysięczny';
473 }
474 my $prefix = SLOWNIE($thousands * 1_000, 0);
475 return $prefix . num2pol_ordinal($remainder);
476 }
477  
478 if ($number >= 100) {
479 my $h = int($number / 100) * 100;
480 my $remainder = $number % 100;
481 if ($remainder == 0) {
482 return $hundreds_ord{$h};
483 }
484 # Cardinal hundreds prefix + ordinal of remainder
485 my $cardinal_prefix = Setki(sprintf('%03d', $number));
486 return $cardinal_prefix . num2pol_ordinal($remainder);
487 }
488  
489 # 20-99 compound
490 if ($number >= 20) {
491 my $t = int($number / 10) * 10;
492 my $remainder = $number % 10;
493 if ($remainder == 0) {
494 return $tens_ord{$t};
495 }
496 return $tens_ord{$t} . ' ' . $base{$remainder};
497 }
498  
499 # Should not reach here
500 return;
501 }
502  
503 # }}}
504  
505 # {{{ capabilities declare supported features
506  
507 sub capabilities {
508 return {
509 cardinal => 1,
510 ordinal => 1,
511 };
512 }
513  
514 # }}}
515 1;
516  
517 # {{{ POD
518  
519 =pod
520  
521 =encoding utf-8
522  
523 =head1 NAME
524  
525 Lingua::POL::Num2Word - Perl module for converting numeric values into their Polish equivalents
526  
527 =head1 VERSION
528  
529 version 0.2603270
530  
531 =head1 DESCRIPTION
532  
533 Number 2 word conversion in POL.
534  
535 This is PetaMem release in iso-639-3 namespace.
536  
537 =head1 SYNOPSIS
538  
539 use Lingua::POL::Num2Word;
540  
541 my $numbers = Lingua::POL::Num2Word->new;
542  
543 my $text = $numbers->parse( 123 );
544  
545 # prints 'sto dwadzieścia trzy'
546 print $text;
547  
548 my $currency = $numbers->currency ( 123.45 );
549  
550 # prints 'sto dwadzieścia trzy zl czterdzieści pięć gr'
551 print $currency;
552  
553 =head1 FUNCTIONS
554  
555 =over
556  
557 =item new
558  
559 Constructor
560  
561 =item parse
562  
563 Converts number into Polish
564  
565 =item Dziesiatki
566  
567 private
568  
569 =item InStr
570  
571 private
572  
573 =item Jednostki
574  
575 private
576  
577 =item KMil
578  
579 private
580  
581 =item KTys
582  
583 private
584  
585 =item Left
586  
587 private
588  
589 =item Mid
590  
591 private
592  
593 =item Right
594  
595 private
596  
597 =item SLOWNIE
598  
599 private
600  
601 =item Setki
602  
603 private
604  
605 =item Trim
606  
607 private
608  
609 =item Val
610  
611 private
612  
613 =item num2pol_ordinal
614  
615 Converts number to Polish ordinal (e.g. 1 => "pierwszy", 21 => "dwudziesty pierwszy").
616  
617 =item currency
618  
619 private
620  
621 =back
622  
623 =head1 KNOWN BUGS
624  
625 None, but that does not mean there are not any.
626  
627 =head1 AUTHORS
628  
629 initial coding:
630 Henrik Steffen E<lt>cpan@topconcepts.deE<gt>
631 specification, maintenance:
632 Richard C. Jelinek E<lt>rj@petamem.comE<gt>
633 maintenance, coding (2025-present):
634 PetaMem AI Coding Agents
635  
636 =head1 LICENSE
637  
638 Original license is not known.
639 PetaMem added Perl 5 licesne as default.
640  
641 =cut
642  
643 # }}}