Spelling of ordinal numbers: Difference between revisions

Added FreeBASIC
(Added Quackery.)
(Added FreeBASIC)
 
(2 intermediate revisions by 2 users not shown)
Line 333:
7891233: seven million eight hundred ninety-one thousand two hundred thirty-third
8007006005004003: eight quadrillion seven trillion six billion five million four thousand third
</pre>
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
 
class SpellingOfOrdinalNumbers
{
private static readonly Dictionary<string, string> ordinalMap = new Dictionary<string, string>
{
{"one", "first"},
{"two", "second"},
{"three", "third"},
{"five", "fifth"},
{"eight", "eighth"},
{"nine", "ninth"},
{"twelve", "twelfth"}
};
 
static void Main(string[] args)
{
long[] tests = new long[] { 1, 2, 3, 4, 5, 11, 65, 100, 101, 272, 23456, 8007006005004003L };
foreach (long test in tests)
{
Console.WriteLine($"{test} = {ToOrdinal(test)}");
}
}
 
private static string ToOrdinal(long n)
{
string spelling = NumToString(n);
string[] split = spelling.Split(' ');
string last = split[split.Length - 1];
string replace;
if (last.Contains("-"))
{
string[] lastSplit = last.Split('-');
string lastWithDash = lastSplit[1];
string lastReplace;
if (ordinalMap.ContainsKey(lastWithDash))
{
lastReplace = ordinalMap[lastWithDash];
}
else if (lastWithDash.EndsWith("y"))
{
lastReplace = lastWithDash.Substring(0, lastWithDash.Length - 1) + "ieth";
}
else
{
lastReplace = lastWithDash + "th";
}
replace = lastSplit[0] + "-" + lastReplace;
}
else
{
if (ordinalMap.ContainsKey(last))
{
replace = ordinalMap[last];
}
else if (last.EndsWith("y"))
{
replace = last.Substring(0, last.Length - 1) + "ieth";
}
else
{
replace = last + "th";
}
}
split[split.Length - 1] = replace;
return string.Join(" ", split);
}
 
private static readonly string[] nums = new string[]
{
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};
 
private static readonly string[] tens = new string[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
 
private static string NumToString(long n)
{
return NumToStringHelper(n);
}
 
private static string NumToStringHelper(long n)
{
if (n < 0)
{
return "negative " + NumToStringHelper(-n);
}
if (n <= 19)
{
return nums[n];
}
if (n <= 99)
{
return tens[n / 10] + (n % 10 > 0 ? "-" + NumToStringHelper(n % 10) : "");
}
string label = null;
long factor = 0;
if (n <= 999)
{
label = "hundred";
factor = 100;
}
else if (n <= 999999)
{
label = "thousand";
factor = 1000;
}
else if (n <= 999999999)
{
label = "million";
factor = 1000000;
}
else if (n <= 999999999999L)
{
label = "billion";
factor = 1000000000;
}
else if (n <= 999999999999999L)
{
label = "trillion";
factor = 1000000000000L;
}
else if (n <= 999999999999999999L)
{
label = "quadrillion";
factor = 1000000000000000L;
}
else
{
label = "quintillion";
factor = 1000000000000000000L;
}
return NumToStringHelper(n / factor) + " " + label + (n % factor > 0 ? " " + NumToStringHelper(n % factor) : "");
}
}
</syntaxhighlight>
{{out}}
<pre>
1 = first
2 = second
3 = third
4 = fourth
5 = fifth
11 = eleventh
65 = sixty-fifth
100 = one hundredth
101 = one hundred first
272 = two hundred seventy-second
23456 = twenty-three thousand four hundred fifty-sixth
8007006005004003 = eight quadrillion seven trillion six billion five million four thousand third
 
</pre>
 
Line 605 ⟶ 762:
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Dim Shared small(19) As String*9 => { _
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", _
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", _
"sixteen", "seventeen", "eighteen", "nineteen" }
Dim Shared tens(9) As String*7 => { "", "", _
"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }
Dim Shared illions(6) As String*12 => {"", _
" thousand", " million", " billion"," trillion", " quadrillion", " quintillion" }
Dim Shared irregularOrdinals(7, 1) As String*7 = { _
{"one", "first"}, {"two", "second"}, {"three", "third"}, {"five", "fifth"}, _
{"eight", "eighth"}, {"nine", "ninth"}, {"twelve", "twelfth"} }
 
Function spell(n As Integer) As String
Dim As String sx, ix, t = ""
Dim As Integer s, i, p
If n < 0 Then
t = "negative "
n = -n
End If
If n < 20 Then
t &= small(n)
Elseif n < 100 Then
t &= tens(n \ 10)
s = n Mod 10
If s > 0 Then t &= "-" & small(s)
Elseif n < 1000 Then
t &= small(n \ 100) & " hundred"
s = n Mod 100
If s > 0 Then t &= " " & spell(s)
Else
sx = ""
i = 0
While n > 0
p = n Mod 1000
n \= 1000
If p > 0 Then
ix = spell(p) & illions(i)
If sx <> "" Then ix &= " " & sx
sx = ix
End If
i += 1
Wend
t &= sx
End If
Return t
End Function
 
Function sayOrdinal(n As Integer) As String
Dim As String s = spell(n)
Dim As String lastWord = ""
Dim As Integer j, i = Len(s)
While i > 0 And Mid(s, i, 1) <> " " And Mid(s, i, 1) <> "-"
lastWord = Mid(s, i, 1) + lastWord
i -= 1
Wend
For j = 0 To Ubound(irregularOrdinals, 1)
If irregularOrdinals(j, 0) = lastWord Then
Return Left(s, i) + irregularOrdinals(j, 1)
End If
Next j
If Right(s, 1) = "y" Then
Return Left(s, Len(s) - 1) + "ieth"
Else
Return s + "th"
End If
End Function
 
Dim As Integer t(0 To ...) = { 1, 2, 3, 4, 5, 11, 65, 100, 101, 272, _
23456, 8007006005004003, 123, 00123.0, 1.23E2 }
For n As Integer = 0 To Ubound(t)
Print sayOrdinal(t(n))
Next n
 
Sleep</syntaxhighlight>
{{out}}
<pre>first
second
third
fourth
fifth
eleventh
sixty-fifth
one hundredth
one hundred first
two hundred seventy-second
twenty-three thousand four hundred fifty-sixth
eight quadrillion seven trillion six billion five million four thousand third
one hundred twenty-third
one hundred twenty-third
one hundred twenty-third</pre>
 
=={{header|Go}}==
Line 2,595 ⟶ 2,845:
 
Note that it is not safe to use this script for numbers with an absolute magnitude >= 2^53 as integers cannot be expressed exactly by Wren's Num type beyond that limit.
<syntaxhighlight lang="ecmascriptwren">var small = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
 
2,130

edits