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

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 27 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 107 CC 13 SCAR 48.3
CC
13
Cov
0%
num2swa_cardinal
182.0
BEGIN
2.0
BEGIN
2.0
CRAP 125.2
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2  
3 package Lingua::SWA::Num2Word;
4 # ABSTRACT: Number to word conversion in Swahili
5  
6 use 5.16.0;
7 use utf8;
8 use warnings;
9  
10 # {{{ use block
11  
12 use Carp;
13 use Export::Attrs;
14 use Readonly;
15  
16 # }}}
17 # {{{ variable declarations
18  
19 my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present';
20 our $VERSION = '0.2603260';
21  
22 # }}}
23  
24 # {{{ num2swa_cardinal convert number to text
25  
26 sub num2swa_cardinal :Export {
27 my $positive = shift;
28  
29 croak 'You should specify a number from interval [0, 999_999_999]'
30 if !defined $positive
31 || $positive !~ m{\A\d+\z}xms
32 || $positive < 0
33 || $positive > 999_999_999;
34  
35 my @ones = qw(sifuri moja mbili tatu nne tano sita saba nane tisa);
36 my @tens = qw(kumi ishirini thelathini arobaini hamsini sitini sabini themanini tisini);
37  
38 return $ones[$positive] if ($positive >= 0 && $positive < 10); # 0 .. 9
39 return 'kumi' if ($positive == 10); # 10
40 return 'kumi na ' . $ones[$positive - 10]
41 if ($positive > 10 && $positive < 20); # 11 .. 19
42  
43 my $out;
44 my $remain;
45  
46 if ($positive > 19 && $positive < 100) { # 20 .. 99
47 my $ten_idx = int($positive / 10) - 1; # tens[0]=kumi, tens[1]=ishirini
48 $remain = $positive % 10;
49  
50 $out = $tens[$ten_idx];
51 $out .= ' na ' . $ones[$remain] if ($remain);
52 }
53 elsif ($positive > 99 && $positive < 1000) { # 100 .. 999
54 my $hun = int($positive / 100);
55 $remain = $positive % 100;
56  
57 $out = 'mia ' . $ones[$hun];
58 $out .= ' na ' . num2swa_cardinal($remain) if ($remain);
59 }
60 elsif ($positive > 999 && $positive < 1_000_000) { # 1000 .. 999_999
61 my $thou = int($positive / 1000);
62 $remain = $positive % 1000;
63  
64 $out = 'elfu ' . num2swa_cardinal($thou);
65 $out .= ' na ' . num2swa_cardinal($remain) if ($remain);
66 }
67 elsif ($positive > 999_999 && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999
68 my $mil = int($positive / 1_000_000);
69 $remain = $positive % 1_000_000;
70  
71 $out = 'milioni ' . num2swa_cardinal($mil);
72 $out .= ' ' . num2swa_cardinal($remain) if ($remain);
73 }
74  
75 return $out;
76 }
77  
78 # }}}
79  
80 1;
81  
82 __END__