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 }

In reply to numeros: Spanish Number Converter by zeno

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.