Welcome to the Monastery PerlMonks

### comment on

 Need Help??
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',
'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

In reply to Re: number 2 text by Cybercosis
in thread number 2 text by Prince99

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

• 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.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2023-03-24 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Which type of climate do you prefer to live in?

Results (61 votes). Check out past polls.

Notices?