| 1 | package Lingua::pt_BR::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 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 10 | 1 | our @EXPORT_OK = qw/num2word/; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =encoding utf8 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Lingua::pt_BR::Nums2Words - Takes a number and gives back its written | ||||||||||||||||||||||||||||
| 17 | form in Brazilian Portuguese | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | use Lingua::pt_BR::Nums2Words ('num2word'); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | print num2word(91) # prints 'noventa e um' | ||||||||||||||||||||||||||||
| 24 | print num2word('19') # prints 'dezenove' | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | print num2word(1000) # prints 'mil' | ||||||||||||||||||||||||||||
| 27 | print num2word(1001) # prints 'mil e um' | ||||||||||||||||||||||||||||
| 28 | print num2word(1_001_001) # prints 'um milhão, mil e um' | ||||||||||||||||||||||||||||
| 29 | print num2word(1_001_250) # prints 'um milhão, mil duzentos e cinquenta' | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Takes a number and gives back its written form in Brazilian | ||||||||||||||||||||||||||||
| 34 | Portuguese. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | B<Note>: 1000 will produce 'mil', and not 'um mil'. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 METHODS | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =cut | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | my %cardinals; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | $cardinals{units} = [undef, qw/um dois três quatro cinco seis sete oito | |||||||||||||||||||||||||||
| 45 | nove/]; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | $cardinals{first_tens} = [undef, qw/onze doze treze quatorze quinze dezesseis | |||||||||||||||||||||||||||
| 48 | dezessete dezoito dezenove/]; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | $cardinals{tens} = [undef, qw/dez vinte trinta quarenta cinquenta sessenta | |||||||||||||||||||||||||||
| 51 | setenta oitenta noventa/]; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | $cardinals{hundreds} = [undef, qw/cento duzentos trezentos quatrocentos | |||||||||||||||||||||||||||
| 54 | quinhentos seiscentos setecentos oitocentos novecentos/]; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | $cardinals{megas} = [undef, qw/mil milh bilh trilh quadrilh quintilh/]; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head2 num2word( $number ) | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Receives a number and returns it written in words. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | my $written_number = nums2words(991); | ||||||||||||||||||||||||||||
| 63 | print $written_number # prints 'novecentos e noventa e um' | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 38 | ▶ | sub num2word { | ||||||||||||||||||||||||||
|
Subroutine
num2word: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 38 | my $number = shift; | |||||||||||||||||||||||||||
| 69 | 38 | ▶ | croak 'No argument provided' unless defined $number; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 37 | ▶ | croak "Not a workable number: $number" unless $number =~ /^\d{1,19}$/x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 33 | ▶ | if ($number == 0) { return 'zero' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 31 | return _solve_triads( _make_triads($number) ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 INTERNALS | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | These methods should not be used directly (unless you know what you're | ||||||||||||||||||||||||||||
| 79 | doing). They are documented here just for the sake of completeness. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head2 _make_triads( $number ) | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Receives a number, splits it in triads (according to the following | ||||||||||||||||||||||||||||
| 84 | examples) and returns a list of triads. Examples: 123 turns to the | ||||||||||||||||||||||||||||
| 85 | list (123). 12345 turns to the list (12, 345). 1234567 turns to the | ||||||||||||||||||||||||||||
| 86 | list (1, 234, 567). | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 31 | ▶ | sub _make_triads { | ||||||||||||||||||||||||||
|
Subroutine
_make_triads: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 91 | 31 | my $number = shift; | |||||||||||||||||||||||||||
| 92 | 31 | my @triads; | |||||||||||||||||||||||||||
| 93 | 31 | ▶ | my $offset = (length $number) % 3 || 3; | ||||||||||||||||||||||||||
|
Condition100%2/2
length($number) % 3 || 3
MC/DC100%1/1
length($number) % 3 || 3
length($number) % 3
Truth table100%2/2
A
A= length($number) % 3
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 31 | ▶ | while (my $triad = substr $number, 0, $offset, '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 81 | push @triads, $triad; | |||||||||||||||||||||||||||
| 97 | 81 | ▶ | if ($offset != 3) { $offset = 3 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 31 | return @triads; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head2 _solve_triads( @triads ) | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Receives a list of triads, calls the function _solve_triad in each of | ||||||||||||||||||||||||||||
| 106 | them and apply the "megas" (millions, billions, trillions). | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 31 | ▶ | sub _solve_triads { | ||||||||||||||||||||||||||
|
Subroutine
_solve_triads: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 111 | 31 | my @triads = @_; | |||||||||||||||||||||||||||
| 112 | 31 | my $megas_counter = $#triads; | |||||||||||||||||||||||||||
| 113 | 31 | my @triads_str; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 31 | for my $triad (@triads) { | |||||||||||||||||||||||||||
| 116 | 81 | ▶ | if ($triad == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 15 | $megas_counter--; | |||||||||||||||||||||||||||
| 118 | 15 | next; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 66 | my $triad_str = _solve_triad($triad); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 66 | ▶ | if ($megas_counter > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 36 | my $mega = $cardinals{megas}->[$megas_counter]; | |||||||||||||||||||||||||||
| 125 | 36 | ▶ | if ($megas_counter > 1) { $mega .= $triad == 1 ? 'ão' : 'ões' } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 126 | 36 | $triad_str .= " $mega"; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 36 | ▶ | if ($triad_str eq 'um mil') { $triad_str = 'mil' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 36 | $megas_counter--; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 66 | push @triads_str, $triad_str; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 31 | my $resp_str; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 31 | ▶ | if (@triads_str == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 10 | $resp_str = $triads_str[0]; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||
| 142 | 21 | $resp_str .= join ', ', @triads_str[0 .. $#triads_str - 1]; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 21 | my $last_triad = $triads[-1]; | |||||||||||||||||||||||||||
| 145 | 21 | my $last_triad_str; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 21 | ▶ | if ($last_triad % 100 == 0 || $last_triad < 100) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$last_triad % 100 == 0 or $last_triad < 100
MC/DC100%2/2
$last_triad % 100 == 0 or $last_triad < 100
$last_triad % 100 == 0
$last_triad < 100
Truth table100%3/3
A or B
A= $last_triad % 100 == 0B= $last_triad < 100
| |||||||||||||||||||||||||||||
| 148 | 13 | $last_triad_str = "e $triads_str[-1]"; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||
| 151 | 8 | $last_triad_str = $triads_str[-1]; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 21 | $resp_str .= " $last_triad_str"; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 31 | return $resp_str; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 _solve_triad( $number ) | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Receives a number with one to three digits (a triad) and returns it | ||||||||||||||||||||||||||||
| 163 | written in words. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 66 | ▶ | sub _solve_triad { | ||||||||||||||||||||||||||
|
Subroutine
_solve_triad: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 168 | 66 | my $number = shift; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 66 | ▶ | if ($number == 100) { return 'cem' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 65 | my $padded_number = sprintf "%03d", $number; | |||||||||||||||||||||||||||
| 173 | 65 | my ($hundreds, $tens, $units) = split '', $padded_number; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 65 | my @resp; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 65 | ▶ | if ($hundreds) { push @resp, $cardinals{hundreds}->[$hundreds] } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 65 | my $first_tens = $tens . $units; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 65 | ▶ | if ($first_tens > 10 and $first_tens < 20) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$first_tens > 10 and $first_tens < 20
MC/DC100%2/2
$first_tens > 10 and $first_tens < 20
$first_tens > 10
$first_tens < 20
Truth table100%3/3
A and B
A= $first_tens > 10B= $first_tens < 20
| |||||||||||||||||||||||||||||
| 182 | 2 | push @resp, $cardinals{first_tens}->[$units]; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | else { | ||||||||||||||||||||||||||||
| 185 | 63 | ▶ | if ($tens) { push @resp, $cardinals{tens}->[$tens] } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 63 | ▶ | if ($units) { push @resp, $cardinals{units}->[$units] } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 65 | return join ' e ', @resp; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 1 | 1; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Lingua::PT::Nums2Words for pt_PT Portuguese. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Gil Magno E<lt>gils@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 THANKS TO | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Italo Gonçales (cpan:GONCALES) E<lt>italo.goncales@gmail.comE<gt> | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Copyright (C) 2015 by Gil Magno | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 211 | it under the same terms as Perl itself, either Perl version 5.20.1 or, | ||||||||||||||||||||||||||||
| 212 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||