| 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
| |||||||||||||||||||||||||||||
| 66 | 12 | ▶ | return () unless (@numbers); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 12 | my @results = map { _num2word($_) } @numbers; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 12 | ▶ | return wantarray ? @results : $results[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 9 | ▶ | if (int($number) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 14 | my $prefix = ''; | |||||||||||||||||||||||||||
| 109 | 14 | ▶ | if ($number < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 119 | 1530 | my $n = ($number % 1000); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # par tranche de 1000 | ||||||||||||||||||||||||||||
| 122 | 1530 | ▶ | if ($n != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 19 | my $s = _num2words_less_1000($n); | |||||||||||||||||||||||||||
| 124 | 19 | ▶ | if (($s =~ /\s*un\s*/) and ($place == 1)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$s =~ /\s*un\s*/ and $place == 1
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
| |||||||||||||||||||||||||||||
| 125 | # on donne pas le un pour mille | ||||||||||||||||||||||||||||
| 126 | 1 | $result = $major[$place].$result; | |||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||
| 128 | 18 | ▶ | if ($place == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 13 | ▶ | if (($s =~ /cent\s*$/) and ($s !~ /^\s*cent/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$s =~ /cent\s*$/ and not $s =~ /^\s*cent/
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/
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$place > 0 and $plural_possible
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
| |||||||||||||||||||||||||||||
| 139 | 1 | ▶ | if ($s !~ /^\s*un/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||