Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Changing numbers into words

by webchalkboard (Scribe)
on Apr 06, 2006 at 11:11 UTC ( [id://541611]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I need to write a script that will turn numbers, i.e. 28.46 into words, i.e. twenty eight fourty six.

This is for cheque writing, so even better would be twenty eight pounds fourty six pence.

Does anyone know of a Perl function that currently exists for doing this? Or am I going to have to resort to lots of switch statements!

Thanks,
Tom

Learning without thought is labor lost; thought without learning is perilous. - Confucius
Artist Galleries

Replies are listed 'Best First'.
Re: Changing numbers into words
by Samy_rio (Vicar) on Apr 06, 2006 at 11:20 UTC

    Hi webchalkboard, Try this example in module description,

    use strict; use warnings; use Lingua::EN::Numbers qw(American); my $n = new Lingua::EN::Numbers(313721.23); print $n->get_string; __END__ Three-Hundred Thirteen Thousand, Seven-Hundred Twenty-One point Twenty +-Three

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      You appear to be using an obsolete interface. 'American' no longer exists (and it should be 'British' anyway), and num2en is much more appropriate than the object.

      Here's how it would look like using a modern vesion of the module, including the addition of pounds and pence as per the OP's request.

      use Lingua::EN::Numbers qw( num2en ); my $en = num2en(313721.23); my ($pounds, $pence) = split(/ point /, $en); if ($pounds eq 'One') { $pounds .= ' pound'; } else $pounds .= ' pounds'; } $pence ||= 'Zero'; $pence .= ' pence'; $en = "$pounds and $pence"; print("$en\n")

      Untested.

Re: Changing numbers into words
by marto (Cardinal) on Apr 06, 2006 at 11:16 UTC
Re: Changing numbers into words
by displeaser (Hermit) on Apr 06, 2006 at 16:11 UTC
Re: Changing numbers into words
by webchalkboard (Scribe) on Apr 06, 2006 at 14:05 UTC

    No sorry didn't search first... will make sure I do next time, i'm usually quite good at that, I guess this time I didn't think my request was all that common, but I should have checked first. Atleast now there is another thread with the same info in so users in the future will find the answers even easier :)

    Many thanks,
    Tom

    Learning without thought is labor lost; thought without learning is perilous. - Confucius
    Art Gallery
Re: Changing numbers into words
by TedPride (Priest) on Apr 06, 2006 at 21:39 UTC
    Just for the heck of it...
    use strict; use warnings; my (@units, @numbers, @teens, @tens, @thousands, @nums, $i); @units = qw/pound pounds pence pence/; @nums = qw/zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen e +ighteen nineteen/; @tens = qw/ten twenty thirty forty fifty sixty seventy eighty ninety/; @thousands = qw/thousand million billion trillion/; for $i (1..8) { push @nums, $tens[$i]; push @nums, "$tens[$i]-$nums[$_]" for 1..9; } for $i (1..9) { push @nums, "$nums[$i] hundred"; push @nums, "$nums[$i] hundred $nums[$_]" for 1..99; } sub wordnum { my ($n, $num, $dec, $bunit, $sunit, @num, $out, $i) = $_[0]; ($num, $dec) = split /\./, $n; $bunit = ($num == 1 ? $units[0] : $units[1]); $sunit = ($dec == 1 ? $units[2] : $units[3]); $dec = "$bunit and $nums[$dec] $sunit"; return "$nums[0] $dec" if $num == 0; $out = $dec; $num -= 0; $num = ' ' . $num while length($num) % 3; push @num, $1 while $num =~ /(...)/g; @num = reverse @num; for ($i = 0; $i <= $#num; $i++) { next if $num[$i] == 0; $out = $nums[$num[$i]] . ($i ? " $thousands[$i-1]" : '') . ' ' + . $out; } return $out; } for (1..20) { $_ = (int rand 5000) . '.' . (int rand 100); print "$_ = " . wordnum($_) . "\n"; }
    I imagine there are ways to do various parts of this much prettier. For instance, dividing a number into chunks of three digits starting from the right. But the code is functional.
Re: Changing numbers into words
by Ido (Hermit) on Apr 06, 2006 at 16:23 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (9)
As of 2024-04-19 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found