blib/lib/Lingua/FR/Nums2Words.pm

filter: Statementstmt 98.6% 72 / 73 Branchbran 93.7% 30 / 32 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 96.9% 125 / 129 CC 20 SCAR 21.6
CC
20
Cov
97%
_num2word
14.0
num2word
4.0
_num2words_less_1000
4.0
CRAP 8.6
↑ Summary
1 package Lingua::FR::Nums2Words;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 @ISA = qw(Exporter);
9  
10 1 %EXPORT_TAGS = ( 'all' => [ qw(
11 num2word
12 ) ] );
13  
14 1 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
15  
16 1 our $VERSION = '1.01';
17  
18 1 my @major = (
19 "",
20 " mille",
21 " million",
22 " milliard",
23 " trillion",
24 " quadrillion",
25 " quintillion"
26 );
27  
28 1 my @ten = (
29 "",
30 " dix",
31 " vingt",
32 " trente",
33 " quarante",
34 " cinquante",
35 " soixante",
36 " soixante-dix",
37 " quatre-vingt",
38 " quatre-vingt-dix"
39 );
40  
41 1 my @num = (
42 "",
43 " un",
44 " deux",
45 " trois",
46 " quatre",
47 " cinq",
48 " six",
49 " sept",
50 " huit",
51 " neuf",
52 " dix",
53 " onze",
54 " douze",
55 " treize",
56 " quatorze",
57 " quinze",
58 " seize",
59 " dix-sept",
60 " dix-huit",
61 " dix-neuf"
62 );
63  
64 12 ▶sub num2word {
Subroutine num2word: called CC=4 SCAR=13.9
Pod undocumented
65 12 ▶ my @numbers = wantarray ? @_ : shift;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 11 12
66 12 ▶ return () unless (@numbers);
Branch: 1 branch
BranchTrueFalseTotal
unless @numbers 0 12 12
67  
68 12 my @results = map { _num2word($_) } @numbers;
69  
70 12 ▶ return wantarray ? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 11 12
71 }
72  
73 19 ▶sub _num2words_less_1000 {
Subroutine _num2words_less_1000: called CC=4 SCAR=13.9
74 19 my ($number) = @_;
75  
76 19 my $result;
77 19 ▶ if ($number % 100 < 20){
Branch: 1 branch
BranchTrueFalseTotal
if ($number % 100 < 20) { } 12 7 19
78 # 19 et moins
79 12 $result = $num[$number % 100];
80 12 $number /= 100;
81 } else {
82 # 9 et moins
83 7 $result = $num[$number % 10];
84 7 $number /= 10;
85  
86 # 90, 80, ... 20
87 7 $result = $ten[$number % 10].$result;
88 7 $number /= 10;
89 }
90  
91 # reste les centaines
92 # y'en a pas
93 19 ▶ if (int($number) == 0) { return $result; }
Branch: 1 branch
BranchTrueFalseTotal
if (int $number == 0) 10 9 19
94  
95 9 ▶ if (int($number) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $number == 1) { } 3 6 9
96 # on ne retourne "un cent xxxx" mais "cent xxxx"
97 3 return " cent$result";
98 } else {
99 6 return $num[$number]." cent".$result;
100 }
101 }
102  
103 15 ▶sub _num2word {
Subroutine _num2word: called CC=14 SCAR=26.4
104 15 my ($number) = @_;
105  
106 15 ▶ return 'zéro' if ($number == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $number == 0 1 14 15
107  
108 14 my $prefix = '';
109 14 ▶ if ($number < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 0) 2 12 14
110 2 $prefix = 'moins';
111 2 $number = -$number;
112 }
113  
114 14 my $place = 0;
115 14 my $result = '';
116 14 my $plural_possible = 1;
117 14 my $plural_form = 0;
118 14 ▶ while ($number > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > 0) 1530 14 1544
119 1530 my $n = ($number % 1000);
120  
121 # par tranche de 1000
122 1530 ▶ if ($n != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != 0) 19 1511 1530
123 19 my $s = _num2words_less_1000($n);
124 19 ▶ if (($s =~ /\s*un\s*/) and ($place == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\s*un\s*/ and $place == 1) { } 1 18 19
Condition100%3/3
$s =~ /\s*un\s*/ and $place == 1
!ll&&!rl&&r
1531
MC/DC100%2/2
$s =~ /\s*un\s*/ and $place == 1
$s =~ /\s*un\s*/ $place == 1
Truth table100%3/3
A and B
A= $s =~ /\s*un\s*/B= $place == 1
ABresult
0X0
100
111
125 # on donne pas le un pour mille
126 1 $result = $major[$place].$result;
127 } else {
128 18 ▶ if ($place == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($place == 0) 13 5 18
129 13 ▶ if (($s =~ /cent\s*$/) and ($s !~ /^\s*cent/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /cent\s*$/ and not $s =~ /^\s*cent/) { } 1 12 13
Condition100%3/3
$s =~ /cent\s*$/ and not $s =~ /^\s*cent/
!ll&&!rl&&r
1111
MC/DC100%2/2
$s =~ /cent\s*$/ and not $s =~ /^\s*cent/
$s =~ /cent\s*$/ not $s =~ /^\s*cent/
Truth table100%3/3
A and not(B)
A= $s =~ /cent\s*$/B= not $s =~ /^\s*cent/
ABresult
0X0
100
111
130 # nnn200 ... nnn900 avec "s"
131 1 $plural_form = 1;
132 } else {
133 # pas de "s" jamais
134 12 $plural_possible = 0;
135 }
136 }
137  
138 18 ▶ if (($place > 0) and ($plural_possible)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($place > 0 and $plural_possible) 1 17 18
Condition100%3/3
$place > 0 and $plural_possible
!ll&&!rl&&r
1341
MC/DC100%2/2
$place > 0 and $plural_possible
$place > 0 $plural_possible
Truth table100%3/3
A and B
A= $place > 0B= $plural_possible
ABresult
0X0
100
111
139 1 ▶ if ($s !~ /^\s*un/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $s =~ /^\s*un/) { } 1 0 1
140 # avec "s"
141 1 $plural_form = 1;
142 } else {
143 # jamais de "s"
144 0 $plural_possible = 0;
145 }
146 }
147  
148 18 $result = $s.$major[$place].$result;
149 }
150 }
151  
152 1530 $place++;
153 1530 $number /= 1000;
154 }
155  
156 14 $result = _trim($prefix.$result);
157  
158 14 ▶ return $plural_form ? $result.'s' : $result;
Branch: 1 branch
BranchTrueFalseTotal
$plural_form ? : 2 12 14
159 }
160  
161 14 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
162 14 my ($string) = @_;
163  
164 14 $string =~ s/^\s+//;
165 14 $string =~ s/\s+$//;
166  
167 14 return $string;
168 }
169  
170 1 1;
171  
172 __END__
↑ Summary