blib/lib/Lingua/FR/Numbers.pm

filter: Statementstmt 95.2% 119 / 125 Branchbran 93.9% 62 / 66 Conditioncond 96.9% 32 / 33 MC/DCmcdc 94.4% 17 / 18 Subroutinesub 83.3% 10 / 12 Podpod 100% 6 / 6 totaltotal 94.6% 246 / 260 SCAR 38.3
CC
46
Cov
95%
number_to_fr
41.0
parse
6.0
ordinate_to_fr
5.0
CRAP 46.3
↑ Summary
1 package Lingua::FR::Numbers;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6 ▶use utf8; # some accents here and there
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use vars qw( $VERSION @ISA @EXPORT_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 use vars qw(
9 $MODE
10 %NUMBER_NAMES
11 %ORDINALS
12 $OUTPUT_DECIMAL_DELIMITER
13 $SIGN_NAMES
14 6 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 $VERSION = '1.161910';
17 6 @ISA = qw(Exporter);
18 6 @EXPORT_OK = qw( &number_to_fr &ordinate_to_fr );
19 6 $SIGN_NAMES = ('moins');
20 6 $OUTPUT_DECIMAL_DELIMITER = ('virgule');
21 6 %NUMBER_NAMES = (
22 0 => 'zéro',
23 1 => 'un',
24 2 => 'deux',
25 3 => 'trois',
26 4 => 'quatre',
27 5 => 'cinq',
28 6 => 'six',
29 7 => 'sept',
30 8 => 'huit',
31 9 => 'neuf',
32 10 => 'dix',
33 11 => 'onze',
34 12 => 'douze',
35 13 => 'treize',
36 14 => 'quatorze',
37 15 => 'quinze',
38 16 => 'seize',
39 17 => 'dix-sept',
40 18 => 'dix-huit',
41 19 => 'dix-neuf',
42 20 => 'vingt',
43 30 => 'trente',
44 40 => 'quarante',
45 50 => 'cinquante',
46 60 => 'soixante',
47 70 => 'soixante',
48 80 => 'quatre-vingt',
49 90 => 'quatre-vingt',
50 100 => 'cent',
51 1e3 => 'mille',
52 1e6 => 'million',
53 1e9 => 'milliard',
54 1e12 => 'billion', # un million de millions
55 1e18 => 'trillion', # un million de billions
56 1e24 => 'quatrillion', # un million de trillions
57 1e30 => 'quintillion', # un million de quatrillions
58 1e36 => 'sextillion', # un million de quintillions,
59 # the sextillion is the biggest legal unit
60 );
61 6 %ORDINALS = (
62 1 => 'premier',
63 5 => 'cinqu',
64 9 => 'neuv',
65 );
66  
67 sub number_to_fr {
68 209 ▶ my $number = shift;
Subroutine number_to_fr: called CC=41 SCAR=37.1
Pod documented
69 209 my @fr_string = ();
70  
71 # Test if $number is really a number, or return undef, from perldoc
72 # -q numbers
73 209 ▶ if ( $number !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($number =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) 2 207 209
74 2 carp("Invalid number format: '$number'");
75 2 return;
76 }
77  
78 207 ▶ if ( $number > ( 1e75 - 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > "1e+75") 1 206 207
79 1 carp("Number '$number' too big to be represented as string");
80 1 return;
81 }
82  
83 206 ▶ return $NUMBER_NAMES{0} if $number == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 0 2 204 206
84  
85 # Add the 'minus' string if the number is negative.
86 204 ▶ push @fr_string, $SIGN_NAMES if abs $number != $number;
Branch: 1 branch
BranchTrueFalseTotal
if abs $number != $number 6 198 204
87 204 $number = abs $number;
88  
89 # We deal with decimal numbers by calling number2fr twice, once for
90 # the integer part, and once for the decimal part.
91 204 ▶ if ( $number != int $number ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number != int $number) 9 195 204
92  
93 # XXX Ugly Hack.
94 9 ( my $decimal ) = $number =~ /\.(\d+)/;
95  
96 9 push @fr_string, number_to_fr( int $number ), $OUTPUT_DECIMAL_DELIMITER;
97  
98 # Decimal numbers are correctly interpreted
99 # https://github.com/sebthebert/Lingua-FR-Numbers/commit/89b717da8950d183488c6d93c7d5e638628ef13f
100 9 ▶ if ( $decimal =~ s/^(0+([1-9][0-9]*))$/$2/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decimal =~ s/^(0+([1-9][0-9]*))$/$2/) { } 5 4 9
101 5 my $decimal_power = 10**length $1;
102 5 ▶ last unless $decimal_power;
Branch: 1 branch
BranchTrueFalseTotal
unless $decimal_power 0 5 5
103 5 my $fr_decimal;
104 5 $fr_decimal = number_to_fr($decimal) . ' ';
105 5 $fr_decimal .= ordinate_to_fr($decimal_power);
106 5 ▶ $fr_decimal .= 's' if $decimal > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $decimal > 1 3 2 5
107 5 push @fr_string, $fr_decimal;
108 }
109 else {
110 4 push @fr_string, number_to_fr($decimal);
111 }
112  
113 9 return join ( ' ', @fr_string );
114 }
115  
116 # First, we split the number by 1000 blocks
117 # i.e:
118 # $block[0] => 0 .. 999 => centaines
119 # $block[1] => 1000 .. 999_999 => milliers
120 # $block[2] => 1e6 .. 999_999_999 => millions
121 # $block[3] => 1e9 .. 1e12-1 => milliards
122 195 my @blocks;
123 195 ▶ while ($number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number) 440 195 635
124 440 push @blocks, $number % 1000;
125 440 $number = int $number / 1000;
126 }
127 195 @blocks = reverse @blocks;
128  
129 # We then go through each block, starting from the greatest
130 # (..., billions, millions, thousands)
131 195 foreach ( 0 .. $#blocks ) {
132  
133 # No need to spell numbers like 'zero million'
134 440 ▶ next if $blocks[$_] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $blocks[$_] == 0 184 256 440
135  
136 256 my $number = $blocks[$_];
137  
138 # Determine the 'size' of the block
139 256 my $power = 10**( ( $#blocks - $_ ) * 3 );
140 256 my $hundred = int( $blocks[$_] / 100 );
141 256 my $teens = int( $blocks[$_] % 100 / 10 );
142 256 my $units = $blocks[$_] % 10;
143  
144 # Process hundred numbers 'inside' the block
145 # (ie. 235 in 235000 when dealing with thousands.)
146  
147 # Hundreds
148 256 ▶ if ($hundred) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hundred) 86 170 256
149 86 my $fr_hundred;
150  
151 # We don't say 'un cent'
152 86 ▶ $fr_hundred = $NUMBER_NAMES{$hundred} . ' '
Branch: 1 branch
BranchTrueFalseTotal
unless $hundred == 1 58 28 86
153 unless $hundred == 1;
154  
155 86 $fr_hundred .= $NUMBER_NAMES{100};
156  
157 # Cent prend un 's' quand il est multiplié par un autre
158 # nombre et qu'il termine l'adjectif numéral.
159 86 ▶ $fr_hundred .= 's'
Branch: 1 branch
BranchTrueFalseTotal
if $hundred > 1 and not $teens and not $units and $_ == $#blocks 3 83 86
Condition100%9/9
$hundred > 1 and not $teens and not $units and $_ == $#blocks
!ll&&!rl&&r
7943
$hundred > 1 and not $teens and not $units
!ll&&!rl&&r
7367
$hundred > 1 and not $teens
!ll&&!rl&&r
284513
MC/DC100%4/4
$hundred > 1 and not $teens and not $units and $_ == $#blocks
$hundred > 1 not $teens not $units $_ == $#blocks
Truth table100%5/5
A and not(B) and not(C) and D
A= $hundred > 1B= not $teensC= not $unitsD= $_ == $#blocks
ABCDresult
0XXX0
10XX0
110X0
11100
11111
160 if ( $hundred > 1 && !$teens && !$units && $_ == $#blocks );
161  
162 86 push @fr_string, $fr_hundred;
163 }
164  
165 # Process number below 100
166 256 my $fr_decimal;
167  
168 # No tens
169 256 ▶ $fr_decimal = $NUMBER_NAMES{$units}
Branch: 1 branch
BranchTrueFalseTotal
if $units and not $teens and not $number == 1 && $power == 1000 73 183 256
Condition100%9/9
$units and not $teens and not $number == 1 && $power == 1000
!ll&&!rl&&r
1671673
$units and not $teens
!ll&&!rl&&r
5611189
$number == 1 && $power == 1000
!ll&&!rl&&r
502316
MC/DC100%4/4
$units and not $teens and not $number == 1 && $power == 1000
$units not $teens $number == 1 $power == 1000
Truth table100%5/5
A and not(B) and not(C && D)
A= $unitsB= not $teensC= $number == 1D= $power == 1000
ABCDresult
0XXX0
10XX0
11110
110X1
11101
170 if ( $units && !$teens )
171 && # On ne dit pas 'un mille' (A bit awkward to put here)
172 !( $number == 1 && ( $power == 1000 ) );
173  
174 # Cas spécial pour les 80
175 # On dit 'quatre-vingts' mais 'quatre-vingt-deux'
176 256 ▶ if ( $teens == 8 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($teens == 8) { } 18 238 256
elsif ($teens == 7 or $teens == 9) { } 50 188 238
elsif ($teens) { } 79 109 188
177 $fr_decimal = $units
178 ? $NUMBER_NAMES{ $teens * 10 } . '-' . $NUMBER_NAMES{$units}
179 18 ▶ : $NUMBER_NAMES{ $teens * 10 } . 's';
Branch: 1 branch
BranchTrueFalseTotal
$units ? : 13 5 18
180 }
181  
182 # Cas spécial pour les nombres en 70 et 90
183 ▶ elsif ( $teens == 7 || $teens == 9 ) {
Condition100%3/3
$teens == 7 or $teens == 9
l!l&&r!l&&!r
1634188
MC/DC100%2/2
$teens == 7 or $teens == 9
$teens == 7 $teens == 9
Truth table100%3/3
A or B
A= $teens == 7B= $teens == 9
ABresult
1X1
011
000
184 50 $units += 10;
185 50 ▶ if ( $teens == 7 && $units == 11 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($teens == 7 and $units == 11) { } 4 46 50
Condition100%3/3
$teens == 7 and $units == 11
!ll&&!rl&&r
34124
MC/DC100%2/2
$teens == 7 and $units == 11
$teens == 7 $units == 11
Truth table100%3/3
A and B
A= $teens == 7B= $units == 11
ABresult
0X0
100
111
186 $fr_decimal =
187 4 $NUMBER_NAMES{ $teens * 10 } . ' et ' . $NUMBER_NAMES{$units};
188 }
189 else {
190 $fr_decimal =
191 46 $NUMBER_NAMES{ $teens * 10 } . '-' . $NUMBER_NAMES{$units};
192 }
193  
194 }
195  
196 # Un nombre s'écrit avec un trait d'union sauf s'il est associé
197 # à 'cent' ou à 'mille'; ou s'il est relié par 'et'.
198 # Nombres écrits avec des 'et': 21, 31, 51, 61, 71
199 elsif ($teens) {
200 79 ▶ if ( $teens == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($teens == 1) { } 44 35 79
elsif ($units == 1 or $units == 11) { } 5 30 35
elsif ($units == 0) { } 3 27 30
201 44 $fr_decimal = $NUMBER_NAMES{ $teens * 10 + $units };
202 }
203 ▶ elsif ( $units == 1 || $units == 11 ) {
Condition66%2/3
$units == 1 or $units == 11
l!l&&r!l&&!r
5030
MC/DC50%1/2
$units == 1 or $units == 11
$units == 1 $units == 11
Truth table66%2/3
A or B
A= $units == 1B= $units == 11
ABresult
1X1
011
000
204 $fr_decimal =
205 5 $NUMBER_NAMES{ $teens * 10 } . ' et ' . $NUMBER_NAMES{$units};
206 }
207 elsif ( $units == 0 ) {
208 3 $fr_decimal = $NUMBER_NAMES{ $teens * 10 };
209 }
210 else {
211 $fr_decimal =
212 27 $NUMBER_NAMES{ $teens * 10 } . '-' . $NUMBER_NAMES{$units};
213 }
214 }
215  
216 256 ▶ push @fr_string, $fr_decimal if $fr_decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $fr_decimal 220 36 256
217  
218 # Processing thousands, millions, billions, ...
219 256 ▶ if ( $power >= 1e3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($power >= 1000) 122 134 256
220 122 my $fr_power;
221  
222 122 ▶ if ( exists $NUMBER_NAMES{$power} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Lingua::FR::Numbers::NUMBER_NAMES{$power}) { } 108 14 122
223 108 $fr_power = $NUMBER_NAMES{$power};
224  
225 # Billion, milliard, etc. prennent un 's' au pluriel
226 108 ▶ $fr_power .= 's' if $number > 1 && $power >= 1e6;
Branch: 1 branch
BranchTrueFalseTotal
if $number > 1 and $power >= 1000000 39 69 108
Condition100%3/3
$number > 1 and $power >= 1000000
!ll&&!rl&&r
264339
MC/DC100%2/2
$number > 1 and $power >= 1000000
$number > 1 $power >= 1000000
Truth table100%3/3
A and B
A= $number > 1B= $power >= 1000000
ABresult
0X0
100
111
227  
228 108 push @fr_string, $fr_power;
229 }
230  
231 # If the power we're looking dealing with doesn't exists
232 # (ie. 1e15, 1e21) we multiply by the lowest power we have,
233 # starting at 1e6.
234 else {
235 14 my $sub_power;
236 14 my $pow_diff = 1;
237 do {
238 22 $pow_diff *= 1_000_000;
239 22 $sub_power = $power / $pow_diff;
240 14 ▶ } until exists $NUMBER_NAMES{$sub_power};
Branch: 1 branch
BranchTrueFalseTotal
exists $Lingua::FR::Numbers::NUMBER_NAMES{$sub_power} or do { do { $pow_diff *= 1000000; $sub_power = $power / $pow_diff } } 8 14 22
241  
242 # If the power_diff doesn't exists (for really big
243 # numbers), we do the same dance.
244 14 ▶ unless ( exists $NUMBER_NAMES{$pow_diff} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Lingua::FR::Numbers::NUMBER_NAMES{$pow_diff}) 0 14 14
245  
246 }
247 14 $fr_power = $NUMBER_NAMES{$pow_diff};
248 14 ▶ $fr_power .= 's' if $number > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $number > 1 10 4 14
249 14 $fr_power .= " de $NUMBER_NAMES{$sub_power}s";
250  
251 # XXX Ugly hack - some architecture output "million de billion" instead of "trillion"
252 14 $fr_power =~ s/million(s)? de billions?/trillion$1/g;
253  
254 14 push @fr_string, $fr_power;
255 }
256 }
257  
258 256 next;
259 }
260  
261 195 return join ( ' ', @fr_string );
262 }
263  
264 sub ordinate_to_fr {
265 25 ▶ my $number = shift;
Subroutine ordinate_to_fr: called CC=5 SCAR=16.1
Pod documented
266  
267 25 ▶ unless ( $number > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($number > 0) 3 22 25
268 3 carp('Ordinates must be strictly positive');
269 3 return;
270 }
271 22 ▶ return $ORDINALS{1} if $number == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 1 1 21 22
272  
273 21 my $ordinal = number_to_fr($number);
274 21 my $last_digit = $number % 10;
275  
276 21 ▶ if ( $last_digit != 1 && exists $ORDINALS{$last_digit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_digit != 1 and exists $Lingua::FR::Numbers::ORDINALS{$last_digit}) 5 16 21
Condition100%3/3
$last_digit != 1 and exists $Lingua::FR::Numbers::ORDINALS{$last_digit}
!ll&&!rl&&r
3135
MC/DC100%2/2
$last_digit != 1 and exists $Lingua::FR::Numbers::ORDINALS{$last_digit}
$last_digit != 1 exists $Lingua::FR::Numbers::ORDINALS{$last_digit}
Truth table100%3/3
A and B
A= $last_digit != 1B= exists $Lingua::FR::Numbers::ORDINALS{$last_digit}
ABresult
0X0
100
111
277 5 my $replace = number_to_fr($last_digit);
278 5 $ordinal =~ s/$replace$/$ORDINALS{$last_digit}/;
279 }
280  
281 21 $ordinal =~ s/e?$/ième/;
282 21 $ordinal =~ s/vingtsième/vingtième/; # Bug #1772
283 21 $ordinal;
284 }
285  
286 #
287 # OO Methods
288 #
289 sub new {
290 76 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
291 76 my $number = shift;
292 76 bless \$number, $class;
293 }
294  
295 sub parse {
296 0 ▶ my $self = shift;
Subroutine parse: not called CC=2 SCAR=17.9
Pod documented
297 0 ▶ if ( $_[0] ) { $$self = shift }
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 0 0 0
298 0 $self;
299 }
300  
301 sub get_string {
302 76 ▶ my $self = shift;
Subroutine get_string: called CC=1 SCAR=0.0
Pod documented
303 76 number_to_fr($$self);
304 }
305  
306 sub get_ordinate {
307 0 ▶ my $self = shift;
Subroutine get_ordinate: not called CC=1 SCAR=6.9
Pod documented
308 0 ordinate_to_fr($$self);
309 }
310  
311 6 1;
312  
313 __END__
↑ Summary