Skip to content

Instantly share code, notes, and snippets.

@Glorfindel83
Last active May 17, 2020 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glorfindel83/6a9eece8c8c23587510676e37f6842be to your computer and use it in GitHub Desktop.
Save Glorfindel83/6a9eece8c8c23587510676e37f6842be to your computer and use it in GitHub Desktop.
// we don't need the zero terms, but indexing is easier this way
const ones = [null, "ūnus", "duo", "trēs", "quattuor", "quīnque", "sex", "septem", "octō", "novem"];
const tenPlusOnes = [null, "ūndecim", "duodecim", "trēdecim", "quattuordecim", "quīndecim", "sēdecim", "septendecim", null, null];
const tens = [null, "decem", "vīgintī", "trīgintā", "quadrāgintā", "quīnquāgintā", "sexāgintā", "septuāgintā", "octōgintā", "nōnāgintā"];
const hundreds = [null, "centum", "ducentī", "trecentī", "quadringentī", "quīngentī", "sescentī", "septingentī", "octingentī", "nōngentī"];
function cardinalNumberLessThanThousand(input) {
if (input <= 0 || input >= 1000)
return null;
var result = "", pending = "";
let digits = input.toString().split("").map(x=>+x).reverse();
digits.push(0); digits.push(0); // for carrying
// position = n for 10^n digit
for (position = 0; position < digits.length; position++) {
let digit = digits[position];
if (digit == 0)
continue;
var term = null;
switch (position) {
case 0: // ones
if ((digit == 8 || digit == 9) && digits[1] != 0 && digits[1] != 9) {
pending = digit == 8 ? "duodē" : "ūndē";
digits[1]++;
} else if (digits[1] == 1) {
term = tenPlusOnes[digit];
digits[1] = 0;
} else {
term = ones[digit];
}
break;
case 1: // tens
term = tens[digit];
break;
case 2: // hundreds
term = hundreds[digit];
break;
}
if (term != null) {
result = pending + term + (result == "" ? "" : (" " + result));
pending = "";
}
}
return result;
}
function cardinalNumber(input) {
if (input == 0)
return "nihil";
if (input < 0 || input >= 1000000)
return "";
// Last part (last three digits)
let remainder = input % 1000;
var result = cardinalNumberLessThanThousand(remainder);
if (input < 1000)
return result;
if (result == null) {
result = "";
} else {
result = " " + result;
}
// First part
let thousands = Math.floor(input / 1000);
if (thousands == 1) {
result = "mīlle" + result;
} else {
result = cardinalNumberLessThanThousand(thousands) + " mīlia" + result;
}
return result;
}
var result = "";
for (i = 0; i < 1000000; i++) {
result += i + "," + cardinalNumber(i) + "\n";
}
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment