blib/lib/Lingua/RO/Numbers.pm

filter: Statementstmt 91.3% 159 / 174 Branchbran 83.8% 104 / 124 Conditioncond 57.7% 26 / 45 MC/DCmcdc 42.3% 11 / 26 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 81.9% 314 / 383 CC 81 SCAR 35.5
CC
81
Cov
84%
_ro_to_number
53.3
_number_to_ro
36.3
_add_numbers
6.0
CRAP 35.0
↑ Summary Build »
1 package Lingua::RO::Numbers;
2  
3 #
4 ## See: http://ro.wikipedia.org/wiki/Sistem_zecimal#Denumiri_ale_numerelor
5 #
6  
7 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 require Exporter;
12 4 our @ISA = qw(Exporter);
13 4 our @EXPORT_OK = qw(number_to_ro ro_to_number);
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 Lingua::RO::Numbers - Convert numeric values into their Romanian string equivalents and viceversa
20  
21 =head1 VERSION
22  
23 Version 0.22
24  
25 =cut
26  
27 4 our $VERSION = '0.22';
28  
29 # Numbers => text
30 4 our %DIGITS;
31 4 @DIGITS{'0' .. '19'} = qw(
32 zero unu doi trei patru cinci șase șapte opt nouă zece
33 unsprezece
34 doisprezece
35 treisprezece
36 paisprezece
37 cincisprezece
38 șaisprezece
39 șaptesprezece
40 optsprezece
41 nouăsprezece
42 );
43  
44 # Text => numbers
45 4 our %WORDS;
46 4 @WORDS{map { _remove_diacritics($_) } values %DIGITS} = keys %DIGITS;
47 4 @WORDS{qw(o un doua cin sai ob)} = (1, 1, 2, 5, 6, 8);
48  
49 # Colocvial
50 4 @WORDS{qw(unspe doispe treispe paispe cinspe cinsprezece saispe saptespe saptuspe optspe optuspe nouaspe)} =
51 (11, 12, 13, 14, 15, 15, 16, 17, 17, 18, 18, 19);
52  
53 # This array contains number greater than 99 and it's used to convert numbers into text
54 4 our @BIGNUMS = (
55 {num => 10**2, sg => 'suta', pl => 'sute', fem => 1},
56 {num => 10**3, sg => 'mie', pl => 'mii', fem => 1},
57 {num => 10**6, sg => 'milion', pl => 'milioane'},
58 {num => 10**9, sg => 'miliard', pl => 'miliarde'},
59 {num => 10**12, sg => 'bilion', pl => 'bilioane'},
60 {num => 10**15, sg => 'biliard', pl => 'biliarde'},
61 {num => 10**18, sg => 'trilion', pl => 'trilioane'},
62 {num => 10**21, sg => 'triliard', pl => 'triliarde'},
63 {num => 10**24, sg => 'cvadrilion', pl => 'cvadrilioane'},
64 {num => 10**27, sg => 'cvadriliard', pl => 'cvadriliarde'},
65 {num => 'inf', sg => 'inifinit', pl => 'infinit'},
66 );
67  
68 # This hash is a reversed version of the above array and it's used to convert text into numbers
69 4 our %BIGWORDS = (map { $_->{sg} => $_->{num}, $_->{pl} => $_->{num} } @BIGNUMS);
70  
71 # Change 'suta' to 'sută'
72 4 $BIGNUMS[0]{'sg'} = 'sută';
73  
74 =head1 SYNOPSIS
75  
76 use Lingua::RO::Numbers qw(number_to_ro ro_to_number);
77 print number_to_ro(315);
78 # prints: 'trei sute cincisprezece'
79  
80 print ro_to_number('trei sute douazeci si cinci virgula doi');
81 # prints: 325.2
82  
83 =head1 DESCRIPTION
84  
85 Lingua::RO::Numbers converts arbitrary numbers into human-readable
86 Romanian text and viceversa, converting arbitrary Romanian text
87 into its corresponding numerical value.
88  
89 =head2 EXPORT
90  
91 Nothing is exported by default.
92 Only the functions B<number_to_ro()> and B<ro_to_number()> are exportable.
93  
94 =over
95  
96 =item B<new(;%opt)>
97  
98 Initialize an object.
99  
100 my $obj = Lingua::RO::Numbers->new();
101  
102 is equivalent with:
103  
104 my $obj = Lingua::RO::Numbers->new(
105 diacritics => 1,
106 invalid_number => undef,
107 negative_sign => 'minus',
108 decimal_point => 'virgulă',
109 thousands_separator => '',
110 infinity => 'infinit',
111 not_a_number => 'NaN',
112 );
113  
114 =item B<number_to_ro($number)>
115  
116 Converts a number to its Romanian string representation.
117  
118 # Functional oriented usage
119 $string = number_to_ro($number);
120 $string = number_to_ro($number, %opts);
121  
122 # Object oriented usage
123 my $obj = Lingua::RO::Numbers->new(%opts);
124 $string = $obj->number_to_ro($number);
125  
126 # Example:
127 print number_to_ro(98_765, thousands_separator => q{,});
128 #=> 'nouăzeci și opt de mii, șapte sute șaizeci și cinci'
129  
130 =item B<ro_to_number($text)>
131  
132 Converts a Romanian text into its numeric value.
133  
134 # Functional oriented usage
135 $number = ro_to_number($text);
136 $number = ro_to_number($text, %opts);
137  
138 # Object oriented usage
139 my $obj = Lingua::RO::Numbers->new(%opts);
140 $number = $obj->ro_to_number($text);
141  
142 # Example:
143 print ro_to_number('patruzeci si doi'); #=> 42
144  
145 =back
146  
147 =cut
148  
149 sub new {
150 4 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
151  
152 4 my $self = bless {
153 diacritics => 1,
154 invalid_number => undef,
155 negative_sign => 'minus',
156 decimal_point => 'virgulă',
157 thousands_separator => '',
158 infinity => 'infinit',
159 not_a_number => 'NaN',
160 }, $class;
161  
162 4 foreach my $key (keys %{$self}) {
163 28 ▶ if (exists $opts{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{$key}) 4 24 28
164 4 $self->{$key} = delete $opts{$key};
165 }
166 }
167  
168 4 foreach my $invalid_key (keys %opts) {
169 0 warn "Invalid option: <$invalid_key>";
170 }
171  
172 4 return $self;
173 }
174  
175 # This function it's an interface to a private function
176 # which converts a mathematical number into its Romanian equivalent text.
177 sub number_to_ro {
178 34 ▶ my ($self, $number, %opts);
Subroutine number_to_ro: called CC=3 SCAR=11.1
Pod documented
179  
180 34 ▶ if (ref $_[0] eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq __PACKAGE__) { } 33 1 34
181 33 ($self, $number) = @_;
182 }
183 else {
184 1 ($number, %opts) = @_;
185 1 $self = __PACKAGE__->new(%opts);
186 }
187  
188 34 my $word_number = $self->_number_to_ro($number + 0);
189  
190 34 ▶ if (not $self->{diacritics}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"diacritics"}) 0 34 34
191 0 $word_number = _remove_diacritics($word_number);
192 }
193  
194 # Return the text-number
195 34 $word_number;
196 }
197  
198 # This function it's an interface to a private function
199 # which converts a Romanian text-number into its mathematical value.
200 sub ro_to_number {
201 33 ▶ my ($self, $text, %opts);
Subroutine ro_to_number: called CC=4 SCAR=14.1
Pod documented
202  
203 33 ▶ if (ref $_[0] eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq __PACKAGE__) { } 32 1 33
204 32 ($self, $text) = @_;
205 }
206 else {
207 1 ($text, %opts) = @_;
208 1 $self = __PACKAGE__->new(%opts);
209 }
210  
211 ( # Decode the text unless it is already UTF-8
212 $] >= 5.0080001 ? utf8::is_utf8($text) : do {
213 0 require Encode;
214 0 Encode::is_utf8($text);
215 }
216 )
217 33 ▶ || do {
Branch: 2 branches
BranchTrueFalseTotal
$] >= "5.0080001" ? utf8::is_utf8($text) : do { require Encode; &Encode::is_utf8($text) } or do { require Encode; $text = &Encode::decode_utf8($text) } 3 30 33
$] >= 5.0080001 ? : 33 0 33
218 3 require Encode;
219 3 $text = Encode::decode_utf8($text);
220 };
221  
222 # Return the number
223 33 $self->_ro_to_number($text);
224 }
225  
226 # This function removes the Romanian diacritics from a given text.
227 sub _remove_diacritics {
228 181 ▶ my ($text) = @_;
Subroutine _remove_diacritics: called CC=1 SCAR=0.0
229 181 $text =~ tr{ăâșțî}{aasti};
230 181 $text;
231 }
232  
233 # This functions removes irrelevant characters from a text
234 sub _normalize_text {
235  
236 # Lowercase and remove the diacritics
237 101 ▶ my $text = _remove_diacritics(lc(shift));
Subroutine _normalize_text: called CC=1 SCAR=0.0
238  
239 # Replace irrelevant characters with a space
240 101 $text =~ tr/a-z / /c;
241  
242 # Return the normalized text
243 101 $text;
244 }
245  
246 # This function adds together a list of numbers
247 sub _add_numbers {
248 38 ▶ my (@nums) = @_;
Subroutine _add_numbers: called CC=6 SCAR=17.9
249  
250 38 my $num = 0;
251 38 ▶ while (defined(my $i = shift @nums)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $i = shift @nums)) 81 38 119
252  
253 # When the current number is lower than the next number
254 81 ▶ if (@nums and $i < $nums[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nums and $i < $nums[0]) 23 58 81
Condition100%3/3
@nums and $i < $nums[0]
!ll&&!rl&&r
312723
MC/DC100%2/2
@nums and $i < $nums[0]
@nums $i < $nums[0]
Truth table100%3/3
A and B
A= @numsB= $i < $nums[0]
ABresult
0X0
100
111
255 23 my $n = shift @nums;
256  
257 # This is a special case, where: int(log(1000)/log(10)) == 2
258 23 my $l = log($n) / log(10);
259 23 ▶ if (length($l) == length(int($l))) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $l == length int $l) 1 22 23
260 1 $l = sprintf('%.0f', $l);
261 }
262  
263 # Factor (e.g.: 400 -> 4)
264 23 my $f = int($i / (10**int(log($i) / log(10))));
265  
266 # When the next number is not really next to the current number
267 # e.g.: $i == 400 and $n == 5000 # should produce 405_000 not 45_000
268 23 ▶ if ((my $mod = length($n) % 3) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $mod = length($n) % 3) != 0) 23 0 23
269 23 $f *= 10**(3 - $mod);
270 }
271  
272 # Join the numbers and continue
273 23 $num += 10**int($l) * $f + $n;
274 23 next;
275 }
276  
277 58 $num += $i;
278 }
279  
280 38 $num;
281 }
282  
283 # This function converts a Romanian
284 # text-number into a mathematical number.
285 sub _ro_to_number {
286 33 ▶ my ($self, $text) = @_;
Subroutine _ro_to_number: called CC=35 SCAR=39.8
287  
288 # When no text has been provided
289 33 ▶ if (not defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 33 33
290 0 return;
291 }
292  
293 # If a thousand separator is defined, remove it from text
294 33 ▶ if (defined($self->{thousands_separator}) and length($self->{thousands_separator})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"thousands_separator"} and length $self->{"thousands_separator"}) 0 33 33
Condition33%1/3
defined $self->{"thousands_separator"} and length $self->{"thousands_separator"}
!ll&&!rl&&r
0330
MC/DC0%0/2
defined $self->{"thousands_separator"} and length $self->{"thousands_separator"}
defined $self->{"thousands_separator"} length $self->{"thousands_separator"}
Truth table33%1/3
A and B
A= defined $self->{"thousands_separator"}B= length $self->{"thousands_separator"}
ABresult
0X0
100
111
295 0 $text =~ s/\Q$self->{thousands_separator}\E/ /g;
296 }
297  
298 # Split the text into words
299 33 my @words = split(' ', _normalize_text($text));
300  
301 33 my $dec_point = _normalize_text($self->{decimal_point});
302 33 my $neg_sign = _normalize_text($self->{negative_sign});
303  
304 33 my @nums; # numbers
305 my @decs; # decimal numbers
306  
307 33 my $neg = 0; # bool -- true when the number is negative
308 33 my $adec = 0; # bool -- true after the decimal point
309  
310 33 my $amount = 0; # int -- current number
311 33 my $factor = 1; # int -- multiplication factor
312  
313 33 ▶ if (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) 33 0 33
314  
315 # Check for negative numbers
316 33 ▶ if ($words[0] eq $neg_sign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words[0] eq $neg_sign) 2 31 33
317 2 $neg = 1;
318 2 shift @words;
319 }
320  
321 # Check for infinity and NaN
322 33 ▶ if (@words == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words == 1) 1 32 33
323  
324 # Infinity
325 1 my $inf = _normalize_text($self->{infinity});
326 1 ▶ if ($words[0] eq $inf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words[0] eq $inf) 0 1 1
327 0 ▶ return $neg ? -9**9**9 : 9**9**9;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 0 0
328 }
329  
330 # Not a number
331 1 my $nan = _normalize_text($self->{not_a_number});
332 1 ▶ if ($words[0] eq $nan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words[0] eq $nan) 0 1 1
333 0 return -sin(9**9**9);
334 }
335 }
336 }
337  
338 # Iterate over the @words
339 33 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (@words and do { $factor = exists $Lingua::RO::Numbers::WORDS{$words[0]} ? 1 : ($words[0] =~ s/zeci\z// ? 10 : 0); $factor and do { $amount = shift @words }; $factor } || @words && exists $Lingua::RO::Numbers::BIGWORDS{$words[0]} && do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} } || do { shift @words; next }) 0 33 33
Condition44%4/9
@words and do { $factor = exists $Lingua::RO::Numbers::WORDS{$words[0]} ? 1 : ($words[0] =~ s/zeci\z// ? 10 : 0); $factor and do { $amount = shift @words }; $factor } || @words && exists $Lingua::RO::Numbers::BIGWORDS{$words[0]} && do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} } || do { shift @words; next }
!ll&&!rl&&r
331060
do { $factor = exists $Lingua::RO::Numbers::WORDS{$words[0]} ? 1 : ($words[0] =~ s/zeci\z// ? 10 : 0); $factor and do { $amount = shift @words }; $factor } or @words and exists $Lingua::RO::Numbers::BIGWORDS{$words[0]} and do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} } or do { shift @words; next }
l!l&&r!l&&!r
10600
do { $factor = exists $Lingua::RO::Numbers::WORDS{$words[0]} ? 1 : ($words[0] =~ s/zeci\z// ? 10 : 0); $factor and do { $amount = shift @words }; $factor } or @words and exists $Lingua::RO::Numbers::BIGWORDS{$words[0]} and do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} }
l!l&&r!l&&!r
10600
MC/DC0%0/4
@words and do { $factor = exists $Lingua::RO::Numbers::WORDS{$words[0]} ? 1 : ($words[0] =~ s/zeci\z// ? 10 : 0); $factor and do { $amount = shift @words }; $factor } || @words && exists $Lingua::RO::Numbers::BIGWORDS{$words[0]} && do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} } || do { shift @words; next }
@words do { $factor = exists $Lingua::RO::Numbers::WORDS{$words[0]} ? 1 : ($words[0] =~ s/zeci\z// ? 10 : 0); $factor and do { $amount = shift @words }; $factor } @words and exists $Lingua::RO::Numbers::BIGWORDS{$words[0]} and do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} } do { shift @words; next }
Truth table20%1/5
A and B or C or D
A= @wordsB= do { $factor = exists $Lingua::RO::Numbers::WORDS{$words[0]} ? 1 : ($words[0] =~ s/zeci\z// ? 10 : 0); $factor and do { $amount = shift @words }; $factor }C= @words and exists $Lingua::RO::Numbers::BIGWORDS{$words[0]} and do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} }D= do { shift @words; next }
ABCDresult
0XXX0
10000
11XX1
101X1
10011
340 @words and (
341  
342 # It's a small number (lower than 100)
343 do {
344 106 ▶ $factor = exists($WORDS{$words[0]}) ? 1 : $words[0] =~ s/zeci\z// ? 10 : 0;
Branch: 2 branches
BranchTrueFalseTotal
exists $Lingua::RO::Numbers::WORDS{$words[0]} ? : 69 37 106
$words[0] =~ s/zeci\z// ? : 37 0 37
345 106 ▶ $factor && do { $amount = shift @words };
Branch: 1 branch
BranchTrueFalseTotal
$factor and do { $amount = shift @words } 106 0 106
346 106 ▶ $factor;
Condition0%0/6
@words and exists $Lingua::RO::Numbers::BIGWORDS{$words[0]} and do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} }
!ll&&!rl&&r
000
@words and exists $Lingua::RO::Numbers::BIGWORDS{$words[0]}
!ll&&!rl&&r
000
MC/DC0%0/3
@words and exists $Lingua::RO::Numbers::BIGWORDS{$words[0]} and do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} }
@words exists $Lingua::RO::Numbers::BIGWORDS{$words[0]} do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} }
Truth table0%0/4
A and B and C
A= @wordsB= exists $Lingua::RO::Numbers::BIGWORDS{$words[0]}C= do { $factor = $Lingua::RO::Numbers::BIGWORDS{shift @words} }
ABCresult
0XX0
10X0
1100
1111
347 }
348  
349 # It's a big number (e.g.: milion)
350 or @words && exists($BIGWORDS{$words[0]}) && do {
351 0 $factor = $BIGWORDS{shift @words};
352 }
353  
354 # Ignore invalid words
355 or do {
356 0 shift @words;
357 0 next;
358 }
359 )
360 ) {
361  
362 # Take and multiply the current number
363 my $num =
364 exists($WORDS{$amount})
365 106 ▶ ? $WORDS{$amount} * $factor
Branch: 1 branch
BranchTrueFalseTotal
exists $Lingua::RO::Numbers::WORDS{$amount} ? : 106 0 106
366 : next; # skip invalid words
367  
368 # Check for some word-joining tokens
369 106 ▶ if (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) 99 7 106
370 99 ▶ if ($words[0] eq 'si') { # e.g.: patruzeci si doi
Branch: 1 branch
BranchTrueFalseTotal
if ($words[0] eq "si") 32 67 99
371 32 shift @words;
372 32 $num += $WORDS{shift @words};
373 }
374  
375 99 ▶ if (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) 85 14 99
376 {
377 85 ▶ if ($words[0] eq 'de') { # e.g.: o suta de mii
Branch: 1 branch
BranchTrueFalseTotal
if ($words[0] eq "de") 23 64 87
378 23 shift @words;
379 }
380  
381 87 ▶ if (exists $BIGWORDS{$words[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Lingua::RO::Numbers::BIGWORDS{$words[0]}) 80 7 87
382 80 $num *= $BIGWORDS{shift @words};
383 }
384  
385 87 ▶ if (@words && $words[0] eq 'de') {
Branch: 1 branch
BranchTrueFalseTotal
if (@words and $words[0] eq "de") 2 85 87
Condition100%3/3
@words and $words[0] eq "de"
!ll&&!rl&&r
12732
MC/DC100%2/2
@words and $words[0] eq "de"
@words $words[0] eq "de"
Truth table100%3/3
A and B
A= @wordsB= $words[0] eq "de"
ABresult
0X0
100
111
386 2 redo;
387 }
388 }
389 }
390 }
391  
392 # If we are after the decimal point, store the
393 # numbers in @decs, otherwise store them in @nums.
394 106 ▶ $adec ? push(@decs, $num) : push(@nums, $num);
Branch: 1 branch
BranchTrueFalseTotal
$adec ? : 11 95 106
395  
396 # Check for the decimal point
397 106 ▶ if (@words and $words[0] eq $dec_point) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words and $words[0] eq $dec_point) 5 101 106
Condition100%3/3
@words and $words[0] eq $dec_point
!ll&&!rl&&r
33685
MC/DC100%2/2
@words and $words[0] eq $dec_point
@words $words[0] eq $dec_point
Truth table100%3/3
A and B
A= @wordsB= $words[0] eq $dec_point
ABresult
0X0
100
111
398 5 $adec = 1;
399 5 shift @words;
400 }
401 }
402  
403 # Return undef when no number has been converted
404 33 ▶ @nums || return;
Branch: 1 branch
BranchTrueFalseTotal
@nums or return 0 33 33
405  
406 # Add all the numbers together (if any)
407 33 my $num = _add_numbers(@nums);
408  
409 # If the number contains decimals,
410 # add them at the end of the number
411 33 ▶ if (@decs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@decs) 5 28 33
412  
413 # Special case -- check for leading zeros
414 5 my $zeros = '';
415 5 ▶ while (@decs and $decs[0] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@decs and $decs[0] == 0) 2 5 7
Condition66%2/3
@decs and $decs[0] == 0
!ll&&!rl&&r
052
MC/DC50%1/2
@decs and $decs[0] == 0
@decs $decs[0] == 0
Truth table66%2/3
A and B
A= @decsB= $decs[0] == 0
ABresult
0X0
100
111
416 2 $zeros .= shift(@decs);
417 }
418  
419 5 $num .= '.' . $zeros . _add_numbers(@decs);
420 }
421  
422 # Return the number
423 33 ▶ $neg ? -$num : $num + 0;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 2 31 33
424 }
425  
426 # This function converts numbers
427 # into their Romanian equivalent text.
428 sub _number_to_ro {
429 151 ▶ my ($self, $number) = @_;
Subroutine _number_to_ro: called CC=34 SCAR=35.9
430  
431 151 my @words;
432 151 ▶ if (exists $DIGITS{$number}) { # example: 8
Branch: 7 branches
BranchTrueFalseTotal
if (exists $Lingua::RO::Numbers::DIGITS{$number}) { } 15 136 151
elsif (lc $number eq 'nan') { } 0 136 136
elsif ($number == 9**9**9) { } 0 136 136
elsif ($number < 0) { } 2 134 136
elsif ($number != int $number) { } 5 129 134
elsif ($number >= $Lingua::RO::Numbers::BIGNUMS[0]{'num'}) { } 87 42 129
elsif ($number > 19 and $number < 100) { } 42 0 42
433 15 push @words, $DIGITS{$number};
434 }
435 elsif (lc($number) eq 'nan') { # not a number (NaN)
436 0 return $self->{not_a_number};
437 }
438 elsif ($number == 9**9**9) { # number is infinit
439 0 return $self->{infinity};
440 }
441 elsif ($number < 0) { # example: -43
442 2 push @words, $self->{negative_sign};
443 2 push @words, $self->_number_to_ro(abs($number));
444 }
445 elsif ($number != int($number)) { # example: 0.123 or 12.43
446 5 my $l = length($number) - 2;
447  
448 5 ▶ if ((length($number) - length(int $number) - 1) < 1) { # special case
Branch: 1 branch
BranchTrueFalseTotal
if (length($number) - length(int $number) - 1 < 1) { } 0 5 5
449 0 push @words, $self->_number_to_ro(sprintf('%.0f', $number));
450 }
451 else {
452 5 push @words, $self->_number_to_ro(int $number);
453 5 push @words, $self->{decimal_point};
454  
455 5 $number -= int $number;
456  
457 5 ▶ until ($number == int($number)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($number == int $number) 16 5 21
458 16 $number *= 10;
459 16 $number = sprintf('%.*f', --$l, $number); # because of imprecise multiplication
460 16 ▶ push @words, $DIGITS{0} if $number < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $number < 1 2 14 16
461 }
462 5 push @words, $self->_number_to_ro(int $number);
463 }
464 }
465 elsif ($number >= $BIGNUMS[0]{num}) { # i.e.: >= 100
466 87 foreach my $i (0 .. $#BIGNUMS - 1) {
467 797 my $j = $#BIGNUMS - $i;
468  
469 797 ▶ if ($number >= $BIGNUMS[$j - 1]{num} && $number <= $BIGNUMS[$j]{num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= $Lingua::RO::Numbers::BIGNUMS[$j - 1]{"num"} and $number <= $Lingua::RO::Numbers::BIGNUMS[$j]{"num"}) 87 710 797
Condition66%2/3
$number >= $Lingua::RO::Numbers::BIGNUMS[$j - 1]{"num"} and $number <= $Lingua::RO::Numbers::BIGNUMS[$j]{"num"}
!ll&&!rl&&r
710087
MC/DC50%1/2
$number >= $Lingua::RO::Numbers::BIGNUMS[$j - 1]{"num"} and $number <= $Lingua::RO::Numbers::BIGNUMS[$j]{"num"}
$number >= $Lingua::RO::Numbers::BIGNUMS[$j - 1]{"num"} $number <= $Lingua::RO::Numbers::BIGNUMS[$j]{"num"}
Truth table66%2/3
A and B
A= $number >= $Lingua::RO::Numbers::BIGNUMS[$j - 1]{"num"}B= $number <= $Lingua::RO::Numbers::BIGNUMS[$j]{"num"}
ABresult
0X0
100
111
470 87 my $cat = int $number / $BIGNUMS[$j - 1]{num};
471 87 $number -= $BIGNUMS[$j - 1]{num} * int($number / $BIGNUMS[$j - 1]{num});
472  
473 87 ▶ my @of = $cat <= 2 ? () : do {
Branch: 1 branch
BranchTrueFalseTotal
$cat <= 2 ? : 29 58 87
474 58 ▶ my @w = exists $DIGITS{$cat} ? $DIGITS{$cat} : ($self->_number_to_ro($cat), 'de');
Branch: 1 branch
BranchTrueFalseTotal
exists $Lingua::RO::Numbers::DIGITS{$cat} ? : 26 32 58
475 58 ▶ if (@w > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@w > 2) 32 26 58
476 32 ▶ $w[-2] = 'două' if $w[-2] eq $DIGITS{2};
Branch: 1 branch
BranchTrueFalseTotal
if $w[-2] eq $Lingua::RO::Numbers::DIGITS{2} 4 28 32
477 }
478 58 @w;
479 };
480  
481 87 ▶ if ($cat >= 100 && $cat < 1_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cat >= 100 and $cat < 1000) 28 59 87
Condition66%2/3
$cat >= 100 and $cat < 1000
!ll&&!rl&&r
59028
MC/DC50%1/2
$cat >= 100 and $cat < 1000
$cat >= 100 $cat < 1000
Truth table66%2/3
A and B
A= $cat >= 100B= $cat < 1000
ABresult
0X0
100
111
482 28 my $rest = $cat - 100 * int($cat / 100);
483 28 ▶ if (@of and $rest != 0 and exists $DIGITS{$rest}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@of and $rest != 0 and exists $Lingua::RO::Numbers::DIGITS{$rest}) 5 23 28
Condition83%5/6
@of and $rest != 0 and exists $Lingua::RO::Numbers::DIGITS{$rest}
!ll&&!rl&&r
2215
@of and $rest != 0
!ll&&!rl&&r
0226
MC/DC66%2/3
@of and $rest != 0 and exists $Lingua::RO::Numbers::DIGITS{$rest}
@of $rest != 0 exists $Lingua::RO::Numbers::DIGITS{$rest}
Truth table75%3/4
A and B and C
A= @ofB= $rest != 0C= exists $Lingua::RO::Numbers::DIGITS{$rest}
ABCresult
0XX0
10X0
1100
1111
484 5 splice @of, -1; # remove 'de'
485 }
486 }
487  
488 push @words,
489 $cat == 1 ? ($BIGNUMS[$j - 1]{fem} ? 'o' : 'un', $BIGNUMS[$j - 1]{sg})
490 : $cat == 2 ? ('două', $BIGNUMS[$j - 1]{pl})
491 87 ▶ : (@of, $BIGNUMS[$j - 1]{pl});
Branch: 3 branches
BranchTrueFalseTotal
$cat == 1 ? : 22 65 87
$Lingua::RO::Numbers::BIGNUMS[$j - 1]{'fem'} ? : 18 4 22
$cat == 2 ? : 7 58 65
492  
493 87 ▶ if ($number > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > 0) 73 14 87
494 73 ▶ $words[-1] .= $self->{thousands_separator} if $BIGNUMS[$j]{num} > 1_000;
Branch: 1 branch
BranchTrueFalseTotal
if $Lingua::RO::Numbers::BIGNUMS[$j]{"num"} > 1000 34 39 73
495 73 push @words, $self->_number_to_ro($number);
496 }
497  
498 87 last;
499 }
500 }
501 }
502 ▶ elsif ($number > 19 && $number < 100) { # example: 42
Condition33%1/3
$number > 19 and $number < 100
!ll&&!rl&&r
0042
MC/DC0%0/2
$number > 19 and $number < 100
$number > 19 $number < 100
Truth table33%1/3
A and B
A= $number > 19B= $number < 100
ABresult
0X0
100
111
503 42 my $cat = int $number / 10;
504 push @words, ($cat == 2 ? 'două' : $cat == 6 ? 'șai' : $DIGITS{$cat}) . 'zeci',
505 42 ▶ ($number % 10 != 0 ? ('și', $DIGITS{$number % 10}) : ());
Branch: 3 branches
BranchTrueFalseTotal
$cat == 2 ? : 14 28 42
$cat == 6 ? : 5 23 28
$number % 10 != 0 ? : 37 5 42
506 }
507 else { # doesn't look like a number
508 0 return $self->{invalid_number};
509 }
510  
511 151 ▶ return wantarray ? @words : @words ? join(' ', @words) : ();
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 117 34 151
@words ? : 34 0 34
512 }
513  
514 =head1 AUTHOR
515  
516 Daniel Șuteu, C<< <trizen at protonmail.com> >>
517  
518 =head1 SUPPORT
519  
520 You can find documentation for this module with the perldoc command.
521  
522 perldoc Lingua::RO::Numbers
523  
524 =head1 REPOSITORY
525  
526 L<https://github.com/trizen/Lingua-RO-Numbers>
527  
528 =head1 REFERENCES
529  
530 L<http://ro.wikipedia.org/wiki/Sistem_zecimal#Denumiri_ale_numerelor>
531  
532 =head1 LICENSE AND COPYRIGHT
533  
534 Copyright 2013-2018 Daniel Șuteu.
535  
536 This program is free software; you can redistribute it and/or modify it
537 under the terms of the the Artistic License (2.0). You may obtain a
538 copy of the full license at:
539  
540 L<http://www.perlfoundation.org/artistic_license_2_0>
541  
542 =cut
543  
544 4 1; # End of Lingua::RO::Numbers
545  
546 __END__
↑ Summary Build »