blib/lib/Lingua/FR/Numbers.pm

filter: Statementstmt 95.2% 120 / 126 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% 247 / 261 CC 46 SCAR 34.1
CC
46
Cov
95%
number_to_fr
41.0
parse
6.0
ordinate_to_fr
5.0
CRAP 30.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(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 $MODE
10 %NUMBER_NAMES
11 %ORDINALS
12 $OUTPUT_DECIMAL_DELIMITER
13 $SIGN_NAMES
14 6 );
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 209 ▶sub number_to_fr {
Subroutine number_to_fr: called CC=41 SCAR=37.1
Pod documented
68 209 my $number = shift;
69 209 my @fr_string = ();
70  
71 # Test if $number is really a number, or return undef, from perldoc
72 # -q numbers
73 209 $number =~ s/_//g; # Allow for '_' separating figures
74 209 ▶ if ( $number !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($number =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) 2 207 209
75 2 carp("Invalid number format: '$number'");
76 2 return;
77 }
78  
79 207 ▶ if ( $number > ( 1e75 - 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > "1e+75") 1 206 207
80 1 carp("Number '$number' too big to be represented as string");
81 1 return;
82 }
83  
84 206 ▶ return $NUMBER_NAMES{0} if $number == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 0 2 204 206
85  
86 # Add the 'minus' string if the number is negative.
87 204 ▶ push @fr_string, $SIGN_NAMES if abs $number != $number;
Branch: 1 branch
BranchTrueFalseTotal
if abs $number != $number 6 198 204
88 204 $number = abs $number;
89  
90 # We deal with decimal numbers by calling number2fr twice, once for
91 # the integer part, and once for the decimal part.
92 204 ▶ if ( $number != int $number ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number != int $number) 9 195 204
93  
94 # XXX Ugly Hack.
95 9 ( my $decimal ) = $number =~ /\.(\d+)/;
96  
97 9 push @fr_string, number_to_fr( int $number ), $OUTPUT_DECIMAL_DELIMITER;
98  
99 # Decimal numbers are correctly interpreted
100 # https://github.com/sebthebert/Lingua-FR-Numbers/commit/89b717da8950d183488c6d93c7d5e638628ef13f
101 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
102 5 my $decimal_power = 10**length $1;
103 5 ▶ last unless $decimal_power;
Branch: 1 branch
BranchTrueFalseTotal
unless $decimal_power 0 5 5
104 5 my $fr_decimal;
105 5 $fr_decimal = number_to_fr($decimal) . ' ';
106 5 $fr_decimal .= ordinate_to_fr($decimal_power);
107 5 ▶ $fr_decimal .= 's' if $decimal > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $decimal > 1 3 2 5
108 5 push @fr_string, $fr_decimal;
109 }
110 else {
111 4 push @fr_string, number_to_fr($decimal);
112 }
113  
114 9 return join ( ' ', @fr_string );
115 }
116  
117 # First, we split the number by 1000 blocks
118 # i.e:
119 # $block[0] => 0 .. 999 => centaines
120 # $block[1] => 1000 .. 999_999 => milliers
121 # $block[2] => 1e6 .. 999_999_999 => millions
122 # $block[3] => 1e9 .. 1e12-1 => milliards
123 195 my @blocks;
124 195 ▶ while ($number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number) 440 195 635
125 440 push @blocks, $number % 1000;
126 440 $number = int $number / 1000;
127 }
128 195 @blocks = reverse @blocks;
129  
130 # We then go through each block, starting from the greatest
131 # (..., billions, millions, thousands)
132 195 foreach ( 0 .. $#blocks ) {
133  
134 # No need to spell numbers like 'zero million'
135 440 ▶ next if $blocks[$_] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $blocks[$_] == 0 184 256 440
136  
137 256 my $number = $blocks[$_];
138  
139 # Determine the 'size' of the block
140 256 my $power = 10**( ( $#blocks - $_ ) * 3 );
141 256 my $hundred = int( $blocks[$_] / 100 );
142 256 my $teens = int( $blocks[$_] % 100 / 10 );
143 256 my $units = $blocks[$_] % 10;
144  
145 # Process hundred numbers 'inside' the block
146 # (ie. 235 in 235000 when dealing with thousands.)
147  
148 # Hundreds
149 256 ▶ if ($hundred) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hundred) 86 170 256
150 86 my $fr_hundred;
151  
152 # We don't say 'un cent'
153 86 ▶ $fr_hundred = $NUMBER_NAMES{$hundred} . ' '
Branch: 1 branch
BranchTrueFalseTotal
unless $hundred == 1 58 28 86
154 unless $hundred == 1;
155  
156 86 $fr_hundred .= $NUMBER_NAMES{100};
157  
158 # Cent prend un 's' quand il est multiplié par un autre
159 # nombre et qu'il termine l'adjectif numéral.
160 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
161 if ( $hundred > 1 && !$teens && !$units && $_ == $#blocks );
162  
163 86 push @fr_string, $fr_hundred;
164 }
165  
166 # Process number below 100
167 256 my $fr_decimal;
168  
169 # No tens
170 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
171 if ( $units && !$teens )
172 && # On ne dit pas 'un mille' (A bit awkward to put here)
173 !( $number == 1 && ( $power == 1000 ) );
174  
175 # Cas spécial pour les 80
176 # On dit 'quatre-vingts' mais 'quatre-vingt-deux'
177 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
178 $fr_decimal = $units
179 ? $NUMBER_NAMES{ $teens * 10 } . '-' . $NUMBER_NAMES{$units}
180 18 ▶ : $NUMBER_NAMES{ $teens * 10 } . 's';
Branch: 1 branch
BranchTrueFalseTotal
$units ? : 13 5 18
181 }
182  
183 # Cas spécial pour les nombres en 70 et 90
184 ▶ 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
185 50 $units += 10;
186 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
187 $fr_decimal =
188 4 $NUMBER_NAMES{ $teens * 10 } . ' et ' . $NUMBER_NAMES{$units};
189 }
190 else {
191 $fr_decimal =
192 46 $NUMBER_NAMES{ $teens * 10 } . '-' . $NUMBER_NAMES{$units};
193 }
194  
195 }
196  
197 # Un nombre s'écrit avec un trait d'union sauf s'il est associé
198 # à 'cent' ou à 'mille'; ou s'il est relié par 'et'.
199 # Nombres écrits avec des 'et': 21, 31, 51, 61, 71
200 elsif ($teens) {
201 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
202 44 $fr_decimal = $NUMBER_NAMES{ $teens * 10 + $units };
203 }
204 ▶ 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
205 $fr_decimal =
206 5 $NUMBER_NAMES{ $teens * 10 } . ' et ' . $NUMBER_NAMES{$units};
207 }
208 elsif ( $units == 0 ) {
209 3 $fr_decimal = $NUMBER_NAMES{ $teens * 10 };
210 }
211 else {
212 $fr_decimal =
213 27 $NUMBER_NAMES{ $teens * 10 } . '-' . $NUMBER_NAMES{$units};
214 }
215 }
216  
217 256 ▶ push @fr_string, $fr_decimal if $fr_decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $fr_decimal 220 36 256
218  
219 # Processing thousands, millions, billions, ...
220 256 ▶ if ( $power >= 1e3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($power >= 1000) 122 134 256
221 122 my $fr_power;
222  
223 122 ▶ if ( exists $NUMBER_NAMES{$power} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Lingua::FR::Numbers::NUMBER_NAMES{$power}) { } 108 14 122
224 108 $fr_power = $NUMBER_NAMES{$power};
225  
226 # Billion, milliard, etc. prennent un 's' au pluriel
227 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
228  
229 108 push @fr_string, $fr_power;
230 }
231  
232 # If the power we're looking dealing with doesn't exists
233 # (ie. 1e15, 1e21) we multiply by the lowest power we have,
234 # starting at 1e6.
235 else {
236 14 my $sub_power;
237 14 my $pow_diff = 1;
238 do {
239 22 $pow_diff *= 1_000_000;
240 22 $sub_power = $power / $pow_diff;
241 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 } } 14 8 22
242  
243 # If the power_diff doesn't exists (for really big
244 # numbers), we do the same dance.
245 14 ▶ unless ( exists $NUMBER_NAMES{$pow_diff} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Lingua::FR::Numbers::NUMBER_NAMES{$pow_diff}) 0 14 14
246  
247 }
248 14 $fr_power = $NUMBER_NAMES{$pow_diff};
249 14 ▶ $fr_power .= 's' if $number > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $number > 1 10 4 14
250 14 $fr_power .= " de $NUMBER_NAMES{$sub_power}s";
251  
252 # XXX Ugly hack - some architecture output "million de billion" instead of "trillion"
253 14 $fr_power =~ s/million(s)? de billions?/trillion$1/g;
254  
255 14 push @fr_string, $fr_power;
256 }
257 }
258  
259 256 next;
260 }
261  
262 195 return join ( ' ', @fr_string );
263 }
264  
265 25 ▶sub ordinate_to_fr {
Subroutine ordinate_to_fr: called CC=5 SCAR=16.1
Pod documented
266 25 my $number = shift;
267  
268 25 ▶ unless ( $number > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($number > 0) 3 22 25
269 3 carp('Ordinates must be strictly positive');
270 3 return;
271 }
272 22 ▶ return $ORDINALS{1} if $number == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 1 1 21 22
273  
274 21 my $ordinal = number_to_fr($number);
275 21 my $last_digit = $number % 10;
276  
277 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
278 5 my $replace = number_to_fr($last_digit);
279 5 $ordinal =~ s/$replace$/$ORDINALS{$last_digit}/;
280 }
281  
282 21 $ordinal =~ s/e?$/ième/;
283 21 $ordinal =~ s/vingtsième/vingtième/; # Bug #1772
284 21 $ordinal;
285 }
286  
287 #
288 # OO Methods
289 #
290 76 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
291 76 my $class = shift;
292 76 my $number = shift;
293 76 bless \$number, $class;
294 }
295  
296 0 ▶sub parse {
Subroutine parse: not called CC=2 SCAR=17.9
Pod documented
297 0 my $self = shift;
298 0 ▶ if ( $_[0] ) { $$self = shift }
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 0 0 0
299 0 $self;
300 }
301  
302 76 ▶sub get_string {
Subroutine get_string: called CC=1 SCAR=0.0
Pod documented
303 76 my $self = shift;
304 76 number_to_fr($$self);
305 }
306  
307 0 ▶sub get_ordinate {
Subroutine get_ordinate: not called CC=1 SCAR=6.9
Pod documented
308 0 my $self = shift;
309 0 ordinate_to_fr($$self);
310 }
311  
312 6 1;
313  
314 __END__
↑ Summary