Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

number 2 text

by Prince99 (Scribe)
on Aug 13, 2001 at 23:59 UTC ( #104564=perlquestion: print w/replies, xml ) Need Help??

Prince99 has asked for the wisdom of the Perl Monks concerning the following question:

I am searching for a module to convert numbers, ie 543, to the number as words, Five Hundred Forty Three. Are there any existing libraries to do this?
Prince99

Too Much is never enough...

Replies are listed 'Best First'.
Re: number 2 text
by lhoward (Vicar) on Aug 14, 2001 at 00:08 UTC
      Thanks a bunch. I tried every combination in the search box and nothing came up. Never did try number::spell. Thanks again.

      Prince99

      Too Much is never enough...
Re: number 2 text
by Cybercosis (Monk) on Aug 14, 2001 at 01:12 UTC
    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

Re: number 2 text
by PetaMem (Priest) on May 12, 2002 at 20:54 UTC
    Hi,

    (Now I call *this* node archaeology)

    this is for german, very dumb. But a nice recursive aproach, suitable for numbers up to 999 999 999 with nice german grammar. Easily extendable.

    sub de_number { my $positive = shift; my $out; my @tokens1 = qw(null ein zwei drei vier fünf sechs sieben acht ne +un zehn elf zwölf); my @tokens2 = qw(zwanzig dreissig vierzig fünfzig sechzig siebzig +achtzig neunzig hundert); # my @tokens3 = qw(million milliarde billion billiarde); # Not need +ed now if($positive >= 0 && $positive <= 12) { $out = $tokens1[$positive]; } elsif($positive >= 13 && $positive <= 19) { $out = $tokens1[$positive-10].'zehn'; } elsif($positive >= 20 && $positive <= 100) { my $one_idx = int $positive/10-2; my $ten_idx = $positive-($one_idx+2)*10; $out = $tokens1[$ten_idx].'und' if $ten_idx; $out .= $tokens2[$one_idx]; } elsif($positive >= 101 && $positive <= 999) { my $one_idx = int $positive/100; $out = $tokens1[$one_idx].'hundert'.&de_number($positive-$one_idx* +100); } elsif($positive >= 1000 && $positive <= 999999) { my $one_idx = int $positive/1000; my $tausend = $positive-$one_idx*1000; my $nonull = $tausend ? &de_number($tausend) : ''; $out = &de_number($one_idx).'tausend'.$nonull; } elsif($positive >= 1000000 && $positive <= 999999999) { my $one_idx = int $positive/1000000; my $mio = $positive-$one_idx*1000000; my $nonull = $mio ? ' '.&de_number($mio) : ''; my $one = $one_idx == 1 ? 'e' : ''; $out = &de_number($one_idx).$one.' million'; $out .= 'en' if $one_idx > 1; $out .= $nonull; } return $out; }

    Bye
     PetaMem

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2023-02-08 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (42 votes). Check out past polls.

    Notices?