/************************************************************************
* @Function : getScaleName()
* @Purpose : Get the full name of the Short Scale Numeral System
* Using the Conway-Guy system for forming number prefixes
* Handles power from 0 to 1000
* The largest scale name is, therefore, the number with
* 3,000 zeros (Novenonagintanongentillion)
*
* @Version : 1.00
* @Author : Mohsen Alyafei
* @Date : 11 Mar 2022
* @Param : {number} the power numeral of the base 1000
* e.g. 1 means 1000^1 = 1,000 (thousand)
* e.g. 2 means 1000^2 = 1,000,000 (million)
* e.g. 3 means 1000^3 = 1,000,000,000 (billion)
* e.g. 4 means 1000^4 = 1,000,000,000,000 (trillion)
* e.g. 11 means 1000^11 = decillion
*
* @Returns : {string} The name of the large number
* @Reference : https://en.wikipedia.org/wiki/Names_of_large_numbers
*************************************************************************/
//===================================================================
function getScaleName(power=0) {
if (power<11) return power<2?["","thousand"][power]:["m","b","tr","quadr","quint","sext","sept","oct","non"][power-2]+"illion";
power-=1;
let tensList = [
[ ,[,,,,,,,,,,0]],
["deci" ,[,,,,,,,"n",,"n",0]], // 10
["viginti" ,[,,,"s",,,"s","m",,"m",0]], // 20
["triginta" ,[,,,"s",,,"s","n",,"n",1]], // 30
["quadraginta" ,[,,,"s",,,"s","n",,"n",1]], // 40
["quinquaginta",[,,,"s",,,"s","n",,"n",1]], // 50
["sexaginta" ,[,,,,,,,"n",,"n",1]], // 60
["septuaginta" ,[,,,,,,,"n",,"n",1]], // 70
["octoginta" ,[,,,,,,"x","m",,"m",1]], // 80
["nonaginta" ,[,,,,,,,,,,1]] // 90
],
hundredsList = [
[ ,[,,,,,,,,,]],
["centi" ,[,,,,,,"x","n",,"n"]], // 100
["ducenti" ,[,,,,,,,"n",,"n"]], // 200
["trecenti" ,[,,,"s",,,"s","n",,"n"]], // 300
["quadringenti",[,,,"s",,,"s","n",,"n"]], // 400
["quingenti" ,[,,,"s",,,"s","n",,"n"]], // 500
["sescenti" ,[,,,,,,,"n",,"n"]], // 600
["septingenti" ,[,,,,,,,"n",,"n"]], // 700
["octingenti" ,[,,,,,,"x","m",,"m"]], // 800
["nongenti" ,[,,,,,,,,,]] // 900
],
hund = ~~(power / 100), // Hundred Digit
ten = ~~(power % 100 / 10), // ten Digit
unit = power % 10 % 10, // unit Digit
unitName = ["","un","duo","tre","quattuor","quin","se","septe","octo","nove"][unit], // Get unit Name from Array
tenName = tensList[ten][0], // Get Tens Name from Array
hundName = hundredsList[hund][0]; // Get Hundreds Name from Array
tenName??=""; hundName??=""; // make it an empty string if undefined
// convert ten names ending with "a" to "i" if it was proceeding the "llion" word
if (!hund && tensList[ten][1][10]) tenName = tenName.slice(0,-1)+"i";
// Pickup and add the correct suffix to the unit Name (s,x,n, or m)
if (ten) tenName = (tensList[ten][1][unit] ??="")+tenName;
else if (hund && !ten) hundName = (hundredsList[hund][1][unit]??="")+hundName;
return unitName + tenName + hundName + "llion"; // Create name
}
//===================================================================
//=========================================
// Test Codes
//=========================================
var r=0; // test tracker
r |= test(0,"");
r |= test(1,"thousand");
r |= test(2,"million");
r |= test(3,"billion");
r |= test(4,"trillion");
r |= test(5,"quadrillion");
r |= test(6,"quintillion");
r |= test(7,"sextillion");
r |= test(8,"septillion");
r |= test(9,"octillion");
r |= test(10,"nonillion");
r |= test(11,"decillion");
r |= test(12,"undecillion");
r |= test(13,"duodecillion");
r |= test(14,"tredecillion");
r |= test(15,"quattuordecillion");
r |= test(16,"quindecillion");
r |= test(17,"sedecillion");
r |= test(18,"septendecillion");
r |= test(19,"octodecillion");
r |= test(20,"novendecillion");
r |= test(21,"vigintillion");
r |= test(22,"unvigintillion");
r |= test(23,"duovigintillion");
r |= test(24,"tresvigintillion");
r |= test(25,"quattuorvigintillion");
r |= test(26,"quinvigintillion");
r |= test(27,"sesvigintillion");
r |= test(28,"septemvigintillion");
r |= test(29,"octovigintillion");
r |= test(30,"novemvigintillion");
r |= test(31,"trigintillion");
r |= test(32,"untrigintillion");
r |= test(33,"duotrigintillion");
r |= test(34,"trestrigintillion");
r |= test(35,"quattuortrigintillion");
r |= test(36,"quintrigintillion");
r |= test(37,"sestrigintillion");
r |= test(38,"septentrigintillion");
r |= test(39,"octotrigintillion");
r |= test(40,"noventrigintillion");
r |= test(41,"quadragintillion");
r |= test(51,"quinquagintillion");
r |= test(61,"sexagintillion");
r |= test(71,"septuagintillion");
r |= test(81,"octogintillion");
r |= test(91,"nonagintillion");
r |= test(101,"centillion");
r |= test(102,"uncentillion");
r |= test(111,"decicentillion");
r |= test(112,"undecicentillion");
r |= test(121,"viginticentillion");
r |= test(122,"unviginticentillion");
r |= test(131,"trigintacentillion");
r |= test(141,"quadragintacentillion");
r |= test(151,"quinquagintacentillion");
r |= test(161,"sexagintacentillion");
r |= test(171,"septuagintacentillion");
r |= test(181,"octogintacentillion");
r |= test(191,"nonagintacentillion");
r |= test(201,"ducentillion");
r |= test(251,"quinquagintaducentillion");
r |= test(301,"trecentillion");
r |= test(351,"quinquagintatrecentillion");
r |= test(378,"septenseptuagintatrecentillion");
r |= test(401,"quadringentillion");
r |= test(451,"quinquagintaquadringentillion");
r |= test(454,"tresquinquagintaquadringentillion");
r |= test(501,"quingentillion");
r |= test(601,"sescentillion");
r |= test(701,"septingentillion");
r |= test(801,"octingentillion");
r |= test(901,"nongentillion");
r |= test(999,"octononagintanongentillion");
r |= test(1000,"novenonagintanongentillion");
if (r==0) console.log("All Test Units Passed.");
//================
function test(n,should) {
var result = getScaleName(n);
if (result !== should) {
console.log(`${n} Output : ${result}`);
console.log(`${n} Should be: ${should}`);
return 1;
}
}