#!/usr/bin/perl
use strict;
use warnings;
print "\n"; # The obligatory output beautifier.
my $x; #general varibles
my $y;
my $z;
my $wholenum; #the whole number that the user enters
my $numlength; #the length of the main number
my $beginlength; #the length of the first section of the number (if it's less than 3)
my $worknum; #current section of the number being worked on
my $tempname; #outputted text name of the number, it isn't temp at all
my $tempnum; #digit of tempnum currently being worked on
my $templength; #used if length is > 63, and thus needs 2 large number names
print "Enter number ";
$wholenum = ; #gets the digits from the user, note there is no error checking for non digits
chomp($wholenum); #takes off the \n char
print "\nNumber is: \n$wholenum";
$numlength = length ($wholenum);
print "\nNumber length is $numlength digits";
$beginlength = $numlength % 3; #beginlength is the length of the first section of the number, so 12345 would give 2, since the first section is 12 (sections are where you would put commas in the number)
$worknum = substr($wholenum, 0, $beginlength); #puts the first section into worknum, which is what we store the currently worked on section in
substr($wholenum, 0, $beginlength) = ""; #takes the current section, that is now in worknum, off of the main number
$numlength = length ($wholenum); #gets the new length without the current section
if ($worknum != 0) {name();} #if the current section is 0 there is no reason to come up with the text name, other wise go to it
while ($wholenum > 0) #will loop until the main number is 0
{
$worknum = substr($wholenum, 0, 3); #puts the next 3 digit section into worknum
substr($wholenum, 0, 3) = ""; #takes the 3 digit section off of the main number
$numlength = length ($wholenum); #gets the new length without the current section
name();
}
sub name
{ #main routine, gives the text name for the current worknum (current section)
if ($worknum >= 100)
{ #if the number is over 100 then we will choose a name based on the hundreds place
$tempnum = int($worknum / 100); #this puts the hundreds place digit into tempnum
if ($tempnum == 9) {$tempname = $tempname . "nine "}
elsif ($tempnum == 8) {$tempname = $tempname . "eight "}
elsif ($tempnum == 7) {$tempname = $tempname . "seven "}
elsif ($tempnum == 6) {$tempname = $tempname . "six "}
elsif ($tempnum == 5) {$tempname = $tempname . "five "}
elsif ($tempnum == 4) {$tempname = $tempname . "four "}
elsif ($tempnum == 3) {$tempname = $tempname . "three "}
elsif ($tempnum == 2) {$tempname = $tempname . "two "}
elsif ($tempnum == 1) {$tempname = $tempname . "one "}
$tempname = $tempname . "hundred "; #no matter what the digit was, since we are in this if we know its > 100 and needs the word hundred
substr($worknum, 0, 1) = ""; #takes off the hundreds digit from the tempnum
}
if ($worknum < 100 && $worknum >= 10)
{ #if the tempnum is < 100 and >= 10 then we need to name the tens place (note that we removed the hundreds place digit at the end of the last if, so even if it was say 138, it's now 38, and would go into this if)
if ($worknum > 19)
{ #since the numbers 10-19 don't follow the same naming standard as 20-99 we will handle them seperate
$tempnum = int($worknum / 10);
if ($tempnum == 9) {$tempname = $tempname . "ninety "}
elsif ($tempnum == 8) {$tempname = $tempname . "eighty "}
elsif ($tempnum == 7) {$tempname = $tempname . "seventy "}
elsif ($tempnum == 6) {$tempname = $tempname . "sixty "}
elsif ($tempnum == 5) {$tempname = $tempname . "fifty "}
elsif ($tempnum == 4) {$tempname = $tempname . "forty "}
elsif ($tempnum == 3) {$tempname = $tempname . "thirty "}
elsif ($tempnum == 2) {$tempname = $tempname . "twenty "}
substr($worknum, 0, 1) = ""; #takes off the tens place digit from tempnum, leaves the ones place
}
else
{ #this will handle those special numbers form 10-19
if ($worknum == 19) {$tempname = $tempname . "nineteen "}
elsif ($worknum == 18) {$tempname = $tempname . "eighteen "}
elsif ($worknum == 17) {$tempname = $tempname . "seventeen "}
elsif ($worknum == 16) {$tempname = $tempname . "sixteen "}
elsif ($worknum == 15) {$tempname = $tempname . "fifteen "}
elsif ($worknum == 14) {$tempname = $tempname . "fourteen "}
elsif ($worknum == 13) {$tempname = $tempname . "thirteen "}
elsif ($worknum == 12) {$tempname = $tempname . "twelve "}
elsif ($worknum == 11) {$tempname = $tempname . "eleven "}
elsif ($worknum == 10) {$tempname = $tempname . "ten "}
substr($worknum, 0, 2) = ""; #since the names describe both the tens and ones place digits we take off both of them, no need to name the ones place digit after you named it with 'fourteen'
}
}
if ($worknum < 10 && $worknum > 0)
{ #this will name the ones place digit, if it was a 10-19 then it's allready named by the last if and won't come in here (since worknum is 0 now)
if ($worknum == 9) {$tempname = $tempname . "nine "}
elsif ($worknum == 8) {$tempname = $tempname . "eight "}
elsif ($worknum == 7) {$tempname = $tempname . "seven "}
elsif ($worknum == 6) {$tempname = $tempname . "six "}
elsif ($worknum == 5) {$tempname = $tempname . "five "}
elsif ($worknum == 4) {$tempname = $tempname . "four "}
elsif ($worknum == 3) {$tempname = $tempname . "three "}
elsif ($worknum == 2) {$tempname = $tempname . "two "}
elsif ($worknum == 1) {$tempname = $tempname . "one "}
substr($worknum, 0, 1) = ""; #removes the final digit, the ones place
}
if ($numlength < 64)
{ #this will handle the big number names, as long as it's not more than 63 digits, if it is then you have to compound the names, that'll be handled below
if ($numlength == 3) {$tempname = $tempname . "thousand"}
elsif ($numlength == 6) {$tempname = $tempname . "million"}
elsif ($numlength == 9) {$tempname = $tempname . "billion"}
elsif ($numlength == 12) {$tempname = $tempname . "trillion"}
elsif ($numlength == 15) {$tempname = $tempname . "quadrillion"}
elsif ($numlength == 18) {$tempname = $tempname . "quintillion"}
elsif ($numlength == 21) {$tempname = $tempname . "sextillion"}
elsif ($numlength == 24) {$tempname = $tempname . "septillion"}
elsif ($numlength == 27) {$tempname = $tempname . "octillion"}
elsif ($numlength == 30) {$tempname = $tempname . "nonillion"}
elsif ($numlength == 33) {$tempname = $tempname . "decillion"}
elsif ($numlength == 36) {$tempname = $tempname . "undecillion"}
elsif ($numlength == 39) {$tempname = $tempname . "duodecillion"}
elsif ($numlength == 42) {$tempname = $tempname . "tredecillion"}
elsif ($numlength == 45) {$tempname = $tempname . "quattuordecillion"}
elsif ($numlength == 48) {$tempname = $tempname . "quindecillion"}
elsif ($numlength == 51) {$tempname = $tempname . "sexdecillion"}
elsif ($numlength == 54) {$tempname = $tempname . "septendecillion"}
elsif ($numlength == 57) {$tempname = $tempname . "octodecillion"}
elsif ($numlength == 60) {$tempname = $tempname . "novemdecillion"}
elsif ($numlength == 63) {$tempname = $tempname . "vigintillion"}
}
elsif ($numlength >= 64)
{ #if it's more than 63 digits than you will need to compound the big number names, this will do that
$templength = ($numlength - 3) % 30; #there is a super big number name ever 30 digits after 63 (93, 123, 153...), so you just need to know what the length mod 30 is, to come up with the first part of the compound big number name
#this is the first part of the compound big number name
if ($templength == 3) {$tempname = $tempname . "thousand "}
elsif ($templength == 6) {$tempname = $tempname . "million "}
elsif ($templength == 9) {$tempname = $tempname . "billion "}
elsif ($templength == 12) {$tempname = $tempname . "trillion "}
elsif ($templength == 15) {$tempname = $tempname . "quadrillion "}
elsif ($templength == 18) {$tempname = $tempname . "quintillion "}
elsif ($templength == 21) {$tempname = $tempname . "sextillion "}
elsif ($templength == 24) {$tempname = $tempname . "septillion "}
elsif ($templength == 27) {$tempname = $tempname . "octillion "}
#these are the super big number names, it will pick which ever is needed to add after the regular big number name to make the compound big number name
if ($numlength >= 64 && $numlength < 93) {$tempname = $tempname . "vigintillion"}
elsif ($numlength >= 93 && $numlength < 123) {$tempname = $tempname . "trigintillion"}
elsif ($numlength >= 123 && $numlength < 153) {$tempname = $tempname . "quadragintillion"}
elsif ($numlength >= 153 && $numlength < 183) {$tempname = $tempname . "quinquagintillion"}
elsif ($numlength >= 183 && $numlength < 213) {$tempname = $tempname . "sexagintillion"}
elsif ($numlength >= 213 && $numlength < 243) {$tempname = $tempname . "septuagintillion"}
elsif ($numlength >= 243 && $numlength < 273) {$tempname = $tempname . "octogintillion"}
elsif ($numlength >= 273 && $numlength < 303) {$tempname = $tempname . "nonagintillion"}
elsif ($numlength >= 303 && $numlength < 333) {$tempname = $tempname . "centillion"}
}
if ($wholenum != 0) {$tempname = $tempname . ", ";} #since we didn't add the comma and space after those names it still needs it, but only if there is still non zero digits left in the main number (and thus it != 0)
}
print "\nText:\n$tempname"; #prints the text version of the number, the end result.
print "\n"; # The obligatory output beautifier.
$x = ;