-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigNumbers.js
82 lines (74 loc) · 4.04 KB
/
bigNumbers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Found here: https://codereview.stackexchange.com/questions/244474/large-number-scales-names-generator
/************************************************************************
* @Function : numberScaleNameShortScale()
* @Purpose : Construct full name of the Short Scale Numeral System
* Using the Conway-Guy system for forming number prefixes
*
* @Version : 0.02
* @Author : Mohsen Alyafei
* @Date : 12 Jun 2020
* @Param : {number} [Power=0] the power numeral of the base 1000
* e.g. 1 means 1000^1 = 1,000
* e.g. 2 means 1000^2 = 1,000,000 (million)
* e.g. 3 means 1000^3 = 1,000,000,000 (billion)
*
* @Returns : {string} The name of the large number
* @Example :
* numberScaleNameShortScale(4);
* // => trillion
*
* numberScaleNameShortScale(21);
* // => vigintillion
*
* @Description : Handles power from 0 to 999
* The larget scale name is therefor the umber with
* 3,000 zeros (Novenonagintanongentillion)
* @Reference : https://en.wikipedia.org/wiki/Names_of_large_numbers
*
* For powers n from 1 to 10, prefixes are constructed based on
* standard dictionary entry.
* For larger powers of n (between 11 and 999), prefixes are constructed
* based on the system described by John Horton Conway and Richard K. Guy.
*************************************************************************/
function numberScaleNameShortScale(Power=0) {
// Do this first and get out quick as it is the most used 99% of the time
// You may delete following line if only interested in Powers above 10 (i.e. 1,000^11 and above)
if (Power<11) return ["","thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion"][Power];
Power-=1; // Adjust the sequence above power of 10 as these are now systematic
let TensList = [
["" ,["","","","" ,"","","" ,"" ,"","" ,false]],
["deci" ,["","","","" ,"","","" ,"n","","n",false]], // 10
["viginti" ,["","","","s","","","s","m","","m",false]], // 20
["triginta" ,["","","","s","","","s","n","","n",true ]], // 30
["quadraginta" ,["","","","s","","","s","n","","n",true ]], // 40
["quinquaginta",["","","","s","","","s","n","","n",true ]], // 50
["sexaginta" ,["","","","" ,"","","" ,"n","","n",true ]], // 60
["septuaginta" ,["","","","" ,"","","" ,"n","","n",true ]], // 70
["octoginta" ,["","","","" ,"","","x","m","","m",true ]], // 80
["nonaginta" ,["","","","" ,"","","" ,"" ,"","" ,true ]] // 90
];
let 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
];
let Hund = Math.floor(Power / 100), // Hundred Digit
Ten = Math.floor(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
// convert Ten names ending with "a" to "i" if it was prceeding 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;
if (Hund && !Ten) HundName = HundredsList[Hund][1][Unit]+HundName;
return UnitName + TenName + HundName + "llion"; // Create name
}