Ever needed to write a check in Spain? Or wanted to know how a Spaniard would say 56271? This program converts between numeric numbers and the Spanish translation in words. The pronunciation is up to you (but it sounds just like it's spelled!) - zeno - Barcelona Perlmongers
#!/usr/bin/perl -w
=head1 NAME
numeros - Convert number into Spanish Text (i.e: 501 =
quinientos uno)
=head1 AUTHOR
zeno
=head1 DESCRIPTION
Converts a number into its Spanish text version.
Accepts input either from STDIN or will prompt the user.
=head1 BUGS
Sure to be some. More than likely I have messed up
the grammar rules a bit on this (I was careful, but
I'm not a native Spanish speaker).
=head1 TO DO
- Version in Catalan
- Fix something with "use locale" to be able to handle
commas and decimals
- Allow a command line parameter to determine gender
(una peseta, un euro)
- Version using one really big regular expression
(I like pain).
=cut
use strict;
my $number;
if (0 == (scalar @ARGV)) {
print "Número/Number: ";
$number = <STDIN>;
} else {
$number = $ARGV[0];
}
if ($number !~ m/^\d+$/) {
die "usage: numeros num\nWhere num is made up of " .
"only numbers\nnum sólo puede contener números," .
" no caracteres\n";
}
my $trans = look_up($number);
print (($trans eq "") ? "cero\n" : "$trans\n");
sub look_up {
# gets called recursively
my ($input_number) = @_;
my @digits=qw(uno dos tres cuatro cinco seis siete ocho
nueve diez once doce trece catorce quince
+
dieciséis diecisiete dieciocho diecinueve
veinte veintiún veintidós veintitrés
veinticuatro veinticinco veintiséis
veintisiete veintiocho veintinueve);
my @tens=qw(treinta cuarenta cincuenta sesenta setenta
ochenta noventa);
my $retval;
if ($input_number == 0) {
$retval = "";
} elsif ($input_number < @digits+1) {
$retval = $digits[$input_number-1];
} elsif (int($input_number / 10)-3 < @tens) {
if (($input_number % 10) == 0) {
$retval = $tens[int($input_number / 10)-3];
} else {
$retval = $tens[int($input_number / 10)-3]
." y ". look_up($input_number % 10);
}
} elsif ($input_number < 1000) {
if ($input_number == 100) {
$retval = "cien";
} elsif ($input_number < 199) {
$retval = "ciento " . look_up($input_number % 100);
} elsif (int($input_number / 100) == 5) {
$retval = "quinientos" ." "
. look_up($input_number % 100);
} elsif (int($input_number / 100) == 7) {
$retval = "setecientos" ." "
. look_up($input_number % 100);
} elsif (int($input_number / 100) == 9) {
$retval = "novecientos" ." "
. look_up($input_number % 100);
} else {
$retval = look_up(int($input_number / 100))
. "cientos" ." ". look_up($input_number % 100);
}
} elsif ($input_number < 1000000) {
if ($input_number < 2000) {
$retval = "mil " . look_up($input_number % 1000);
} else {
$retval = look_up(int($input_number / 1000))
. " mil " . look_up($input_number % 1000);
}
} elsif ($input_number < 1000000000000) {
if ($input_number < 2000000) {
$retval = "un millón "
. look_up($input_number % 1000000);
} else {
$retval = look_up(int($input_number / 1000000))
. " millones "
. look_up($input_number % 1000000);
}
} elsif ($input_number < 1000000000000000) {
if ($input_number < 2E12) {
$retval = "un billón "
. look_up($input_number % 1E12);
} else {
$retval = look_up(int($input_number / 1E12))
. " billones "
. look_up($input_number % 1E12);
}
} else {
$retval = "Overflow/Demasiado grande"
. " (limite es 999999999999999)";
}
return $retval
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|