#!/usr/bin/perl -w use strict; my @ten_factors = (['zilch', 'one', 'two', 'three', 'four', 'five', 'six', '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($$trip[1] == 0); } $retval = $ten_factors[0][$$trip[0]] . ' hundred ' . $retval; return $retval; }