This piece of code converts your number into a string. For example: 404 gets "four hundred four", 721 gets "seven hundred twenty-one", and so on.

Note that this is based on the Brittish system because that is close to my native system (Dutch). So maybe some American (oriented) people might think it's wrong, but it's just no American oriented code.

Currently, it only takes positive integer numbers.
That is: $x > 0 && $x / 2 == int($x / 2)
# This is how it works: # resolve(300) # returns "three hundred" # resolve(210) # returns "two hundred ten" # and so on... # Note that this is based on the Brittish system because that is close + to my # native system (Dutch). # So maybe some American (oriented) people might think it's wrong, but + it's # just no American oriented code. # Currently, it only takes positive integer numbers. # That is: $x > 0 && $x / 2 == int($x / 2) sub resolve { my @numbers1 = qw( one two three four five six seven eight nine te +n eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen +nineteen twenty ); my @numbers2 = qw( ten twenty thirty fourty fifty sixty seventy ei +ghty ninety ); my @numbers3 = qw( thousand million milliard billion billiard tril +lion trilliard quadrillion quadrilliard quintillion quintilliard sext +illion sextilliard septillion septilliard octillion octilliard nonill +ian nonilliard decillion decilliard ); my $nr = int $_[0]; $nr = "$nr"; $nr =~ s/\D//g; $nr = $nr ? $nr : "0"; if ($nr eq "0") { return ""; } my ($smallone, $bigone, $cnr, @allnumbers, $resolve); if ($nr < 20) { return $numbers1[$nr-1]; } elsif ($nr < 100) { ($nr, $cnr) = $nr =~ m/^(.)(.)$/; $smallone = &resolve($cnr); $bigone = $numbers2[$nr-1]; return "$bigone" . ($smallone ? "-$smallone" : ""); } elsif ($nr < 1000) { ($nr, $cnr) = $nr =~ m/^(.)(..)$/; $cnr = &resolve($cnr); $nr = &resolve($nr); return "$nr hundred $cnr"; } else { $nr = reverse $nr; $nr =~ s/(\d{3})/$1 /g; $nr = reverse $nr; @allnumbers = split(" ", $nr); while (@allnumbers) { $cnr = shift @allnumbers; $bigone = &resolve($cnr); if ($#allnumbers > -1 && $cnr > 0) { $bigone .= " $numbers3[$#allnumbers] "; } $resolve .= $bigone; } return $resolve; } }

Replies are listed 'Best First'.
Re: Convert a number (for example 404) into a string ("four hundred four")
by jdporter (Paladin) on Apr 07, 2004 at 19:05 UTC
    O.k.... but there's a module on CPAN that does something similar: Number::Spell.

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: Convert a number (for example 404) into a string ("four hundred four")
by Not_a_Number (Prior) on Apr 07, 2004 at 20:31 UTC
    shouldn't 404 be "four hundred and four"?

    Yes, it most certainly should, at least in British English (which the OP explicitly refers to). Similarly, 404404 should be "four hundred and four thousand, four hundred and four".

    Furthermore, there has been no difference in current US and British English usage for 'big' numbers (billion, trillion, etc) for at least 30-40 years. So when you read "three billion dollars" in the British press, it means exactly the same thing as if you had read it in the North American press.

    Trust me. I have been doing financial translations for international banks for more years than I would like to admit to.

    Last but not least, 'milliard', 'billiard', 'trilliard', etc do not exist, at least in common usage (except the second in terms such as 'billiard ball' :). A quick poll of my English-speaking colleagues (all specialists in language|numbers|IT) shows that nobody has ever heard of any of them. And indeed, when they figure in dictionaries, it is always with a mention such as "obsolete" or "'Milliard' is not used in modern English".

    dave

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Convert a number (for example 404) into a string ("four hundred four")
by zentara (Cardinal) on Apr 08, 2004 at 12:51 UTC
    This is a nice one too. number.cgi

    Here is a list of it's options:

    # number - print the English name of a number of any size # # usage: # number [-p] [-l] [-d] [-m] [-c] [-o] [-e] [-h] [number] # # -p input is a power of 10 # -l input is a Latin power (1000^x) # -d add dashes to help with pronunciation # -m output name in a more compact exponential form # -c output number in comma/dot form # -o output number on a single line # -e use European instead of American name system # -h print a help message only # # If number is omitted, then it is read from standard input.

    I'm not really a human, but I play one on earth. flash japh
Re: Convert a number (for example 404) into a string ("four hundred four")
by Anonymous Monk on Apr 07, 2004 at 19:31 UTC
    shouldn't 404 be "four hundred and four"?
      I've looked for that on the internet and I think not, because on the Internet I found the following meaning of 'and':

      People use 'and' to seperate a bigger money unit from a smaller one (I.E. dollar from cent) and then 'four hundred AND four' would be: $400.04 :)
        Well I just double checked with my Iron Maiden "Number of the Beast" CD and they use "Six hundred and sixty-six" for 666. :P
        'four hundred AND four' would be: $400.04

        no it wouldn't. When specifying two units of currency, both have to be specified:

        $400.04
        Four hundred dollars and four cents
        $404.04
        Four hundred and four dollars and four cents
        $4.04
        four dollars and four cents.

        "Four hundred and four" could only be interpreted unambiguously as $404. Or "page not found', it depends on the context :)

Re: Convert a number (for example 404) into a string ("four hundred four")
by Anonymous Monk on Aug 04, 2005 at 12:50 UTC
    $x / 2 == int($x / 2)
    So 5 isn't an integer?