File Coverage

blib/lib/Lingua/FRA/Nums2Words.pm
Criterion Covered Total %
statement 70 71 98.5
branch 27 30 90.0
condition 11 11 100.0
subroutine 10 10 100.0
pod 1 1 100.0
total 119 123 96.7


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; mode:folding -*-
2              
3             package Lingua::FRA::Nums2Words;
4             # ABSTRACT: Number 2 word conversion in FRA.
5              
6             # {{{ use block
7              
8 1     1   27821 use 5.10.1;
  1         4  
  1         42  
9              
10 1     1   5 use strict;
  1         2  
  1         41  
11 1     1   4 use warnings;
  1         11  
  1         52  
12 1     1   6 use utf8;
  1         1  
  1         6  
13              
14 1     1   1081 use Perl6::Export::Attrs;
  1         9995  
  1         6  
15              
16             # }}}
17             # {{{ variables declaration
18              
19             our $VERSION = 0.0682;
20              
21             my @major = (
22             "",
23             " mille",
24             " million",
25             " milliard",
26             " trillion",
27             " quadrillion",
28             " quintillion"
29             );
30              
31             my @ten = (
32             "",
33             " dix",
34             " vingt",
35             " trente",
36             " quarante",
37             " cinquante",
38             " soixante",
39             " soixante-dix",
40             " quatre-vingt",
41             " quatre-vingt-dix"
42             );
43              
44             my @num = (
45             "",
46             " un",
47             " deux",
48             " trois",
49             " quatre",
50             " cinq",
51             " six",
52             " sept",
53             " huit",
54             " neuf",
55             " dix",
56             " onze",
57             " douze",
58             " treize",
59             " quatorze",
60             " quinze",
61             " seize",
62             " dix-sept",
63             " dix-huit",
64             " dix-neuf"
65             );
66              
67             # }}}
68             # {{{ num2word
69              
70             sub num2word :Export {
71 15     15 1 4688 my @numbers = @_;
72 15 50       37 return () unless (@numbers);
73              
74 15         20 my @results = map { _num2word($_) } @numbers;
  15         27  
75              
76 15 50       50 return wantarray ? @results : $results[0];
77 1     1   181 }
  1         2  
  1         3  
78              
79             # }}}
80             # {{{ _num2words_less_1000
81              
82             sub _num2words_less_1000 {
83 25     25   27 my ($number) = @_;
84              
85 25         27 my $result;
86 25 100       48 if ($number % 100 < 20){
87             # 19 et moins
88 10         14 $result = $num[$number % 100];
89 10         17 $number /= 100;
90             } else {
91             # 9 et moins
92 15         21 $result = $num[$number % 10];
93 15         19 $number /= 10;
94              
95             # 90, 80, ... 20
96 15         30 $result = $ten[$number % 10].$result;
97 15         21 $number /= 10;
98             }
99              
100             # reste les centaines
101             # y'en a pas
102 25 100       63 if (int($number) == 0) { return $result; }
  8         18  
103              
104 17 100       23 if (int($number) == 1) {
105             # on ne retourne "un cent xxxx" mais "cent xxxx"
106 3         10 return " cent$result";
107             } else {
108 14         40 return $num[$number]." cent".$result;
109             }
110             }
111              
112             # }}}
113             # {{{ _num2word
114              
115             sub _num2word {
116 15   100 15   33 my $number = shift // 0;
117              
118 15 100       35 return 'zéro' if ($number == 0);
119              
120 13         18 my $prefix = '';
121 13 100       25 if ($number < 0) {
122 2         3 $prefix = 'moins';
123 2         11 $number = -$number;
124             }
125              
126 13         13 my $place = 0;
127 13         15 my $result = '';
128 13         12 my $plural_possible = 1;
129 13         13 my $plural_form = 0;
130 13         29 while ($number > 0) {
131 1324         1379 my $n = ($number % 1000);
132              
133             # par tranche de 1000
134 1324 100       1954 if ($n != 0) {
135 25         38 my $s = _num2words_less_1000($n);
136 25 100 100     81 if (($s =~ /\s*un\s*/) and ($place == 1)) {
137             # on donne pas le un pour mille
138 1         14 $result = $major[$place].$result;
139             } else {
140 24 100       50 if ($place == 0) {
141 11 100 100     51 if (($s =~ /cent\s*$/) and ($s !~ /^\s*cent/)) {
142             # nnn200 ... nnn900 avec "s"
143 1         2 $plural_form = 1;
144             } else {
145             # pas de "s" jamais
146 10         508 $plural_possible = 0;
147             }
148             }
149              
150 24 100 100     76 if (($place > 0) and ($plural_possible)) {
151 2 50       7 if ($s !~ /^\s*un/) {
152             # avec "s"
153 2         4 $plural_form = 1;
154             } else {
155             # jamais de "s"
156 0         0 $plural_possible = 0;
157             }
158             }
159              
160 24 100       50 return if (!defined $major[$place]);
161              
162 23         54 $result = $s.$major[$place].$result;
163             }
164             }
165              
166 1323         1253 $place++;
167 1323         2334 $number /= 1000;
168             }
169              
170 12         40 $result = _trim($prefix.$result);
171              
172 12 100       54 return $plural_form ? $result.'s' : $result;
173             }
174              
175             # }}}
176             # {{{ _trim
177              
178             sub _trim {
179 12     12   17 my ($string) = @_;
180              
181 12         42 $string =~ s/^\s+//;
182 12         32 $string =~ s/\s+$//;
183              
184 12         25 return $string;
185             }
186              
187             # }}}
188              
189             1;
190              
191             __END__