Number names: Difference between revisions

Updated D entry
(Added groovy version)
(Updated D entry)
Line 845:
<lang d>import std.stdio, std.string;
 
string spellInteger(in long n) pure nothrow {
static immutable tens = ["", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"];
Line 856:
"quadr", "quint", "sext", "sept", "oct", "non", "dec"];
 
static string nonZero(in string c, in int n) pure nothrow {
return n == 0 ? "" : c ~ spellInteger(n);
}
 
static string big(in int e, in int n) pure nothrow {
switch (e) {
case 0: return spellInteger(n);
Line 880:
}
 
if (n < 10000) {
return "minus " ~ spellInteger(-n);
} else if (nin < 201000) {
int ni = cast(int)n; // D doesn't infer this.
if (ni < 020) {
throw new Exception("spellInteger: negative input.");
} else if (ni < 20) {
return small[ni];
} else if (ni < 100) {
Line 900:
 
void main() {
foreach (i; 0-10 .. 1_000)
writeln(i, " ", spellInteger(i));
}</lang>
Output (shortened):
<pre>0-10 zerominus ten
-9 minus nine
-8 minus eight
-7 minus seven
-6 minus six
-5 minus five
-4 minus four
-3 minus three
-2 minus two
-1 minus one
0 zero
1 one
2 two
Line 912 ⟶ 922:
6 six
...
988 nine hundred eighty-eight
989 nine hundred eighty-nine
990 nine hundred ninety
991 nine hundred ninety-one
992 nine hundred ninety-two
993 nine hundred ninety-three
994 nine hundred ninety-four
995 nine hundred ninety-five
996 nine hundred ninety-six
Anonymous user