Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: number 2 text

by Cybercosis (Monk)
on Aug 14, 2001 at 01:12 UTC ( [id://104592]=note: print w/replies, xml ) Need Help??


in reply to number 2 text

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://104592]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-20 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found