Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

numeros: Spanish Number Converter

by zeno (Friar)
on May 15, 2002 at 15:05 UTC ( #166740=CUFP: print w/ replies, xml ) Need Help??

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 }

Comment on numeros: Spanish Number Converter
Download Code
Re: numeros: Spanish Number Converter
by Anonymous Monk on Mar 15, 2007 at 20:06 UTC
    There's a "+" in the middle of the "my @digits" (around line 25), so that "6" is translated as "+". Deleted this line takes care of it.
Re: numeros: Spanish Number Converter
by Anonymous Monk on Aug 05, 2009 at 05:22 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://166740]
Approved by particle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2015-07-06 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls