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.

In reply to Re: Changing numbers into words by TedPride
in thread Changing numbers into words by webchalkboard

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.