Erm... this may be a bit late, but I just banged this together:
#!/usr/bin/perl -w
use strict;
my @ten_factors = (['zilch', 'one', 'two', 'three', 'four', 'five', 's
+ix',
'seven', 'eight', 'nine'],
['ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen',
'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy',
'eighty', 'ninety'],
'thousand',
'million',
'billion',
'trillion',
'quadrillion',
'quintillion',
'sextillion',
'septillion',
'octillion');
sub num2word
{
my $num = shift;;
chomp($num);
die "NaN\," if($num !~ /^[0-9]*$/);
$num = '0' . $num while(length($num) % 3);
my $factor = 0;
my $output = '';
while($num)
{
my @trip = reverse (chop($num), chop($num), chop($num));
my $trip = trip_convert(\@trip);
if($factor > 0)
{
$trip .= ' ' . $ten_factors[$factor+1];
}
$output = $trip . ' ' . $output;
$factor++;
}
$output =~ s/zilch [a-z]*//g;
return $output;
}
sub trip_convert
{
my $trip = shift;
my $retval = '';
if($$trip[1] == 1)
{
$retval = $ten_factors[1][$$trip[2]];
}
else
{
$retval = $ten_factors[0][$$trip[2]];
$retval = $ten_factors[1][$$trip[1]+8] . ' ' . $retval unless($$tr
+ip[1] == 0);
}
$retval = $ten_factors[0][$$trip[0]] . ' hundred ' . $retval;
return $retval;
}
It isn't exactly the best I've ever written, but it works (more or less).
~Cybercosis
nemo accipere quod non merere
-
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.