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;
}
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.