· 6 years ago · Dec 07, 2019, 08:44 AM
1-- ModuleScript: PrefixModule.lua
2--[[
3 API:
4 Function Module:SetSettings(REQ type Table)
5 DESC: Overrides the Modules default settings
6 Table must include at least one of the following:
7 - DecimalPlaces:type Number (How many decimal places the Module should round to (2 d.p. will display as 1.00k))
8 returns nil
9 e.g. Module:SetSettings({['DecimalPlaces'] = 2})
10
11 Function Module:NumberToString(REQ type Number)
12 DESC: Converts a Number into a String with a suffix (350,000,000 --> 350m)
13 returns type String
14 e.g. Module:NumberToString(350000000)
15
16 Function Module:StringToNumber(REQ type String)
17 DESC: Converts a string into a number (removing the suffix) (350m --> 350,000,000)
18 returns type Number
19 e.g. Module:StringToNumber(350m)
20
21 Function Module:CreateDateFormat(REQ type Number)
22 DESC: Adds a suffix to the end of a Number which is commonly associated with a date (1 --> 1st)
23 returns type String
24 e.g. Module:CreateDateFormat(1)
25]]
26
27-- EDITABLE
28local SuffixTable = {
29 'k',
30 'M',
31 'B',
32 'T',
33 'Qd',
34 'Qn',
35 'Sx',
36 'Sp',
37 'O',
38 'N',
39 'De',
40 'Ud',
41 'DD',
42 'tdD',
43 'QnD',
44 'SxD',
45 'SpD',
46 'OcD',
47 'NvD',
48 'VgN',
49 'UvG',
50 'DvG',
51 'TvG',
52 'QtV',
53 'QnV',
54 'SeV',
55 'SpG',
56 'OvG',
57 'NvG',
58 'TgN',
59 'UtG',
60 'DtG',
61 'TsTg',
62 'QtTg',
63 'QnTg',
64 'SsTg',
65 'SpTg',
66 'OcTg',
67 'NoTg',
68 'QdDr',
69 'UnAg',
70 'DuAg',
71 'TeAg',
72 'QdAg',
73 'QnAG',
74 'SxAg',
75 'SpAg',
76 'OcAg',
77 'NvAg',
78 'CT'
79}
80
81-- Uneditable below
82
83local Module = {}
84
85Module.SuffixTable = SuffixTable
86
87
88function Module:SetSettings(Table)
89 if not Table or type(Table) ~= 'table' then
90 error('Insufficient arguments')
91 end
92
93 for Setting, Value in pairs(Table) do
94 Module[Setting] = Value -- Update the current module setting
95 end
96end
97
98
99
100
101
102function Module:NumberToString(Number)
103 if not Number or type(Number) ~= 'number' then
104 error('Insufficient arguments')
105 end
106
107
108 if Number < 1000 then -- Number doesn't need a suffix
109 return tostring(Number)
110 end
111
112 for Index = #Module.SuffixTable, 1, -1 do -- Start from the highest value, to the bottom
113 local ShortenedNumber = 10 ^ (Index * 3)
114 if Number >= ShortenedNumber then -- Number is larger or equal to the category it falls under
115 local Suffix = SuffixTable[Index] -- Get the suffix
116 return string.format('%.'..Module['DecimalPlaces']..'f'..Suffix, Number / ShortenedNumber) -- '%.2f' returns 1.00
117 end
118 end
119end
120
121
122
123
124function Module:StringToNumber(String)
125 if not String or type(String) ~= 'string' then
126 error('Insufficient arguments')
127 end
128
129 local TotalMagnitude = 1
130
131 for Key, Suffix in pairs(Module.SuffixTable) do
132 String = string.gsub(String, Suffix, function()
133 TotalMagnitude = TotalMagnitude * (10 ^ (Key * 3))
134 return ""
135 end)
136 end
137
138 return TotalMagnitude * String
139end
140
141
142
143
144
145function Module:CreateDateFormat(Number)
146 if not Number or type(Number) ~= 'number' then
147 error('Insufficient arguments')
148 end
149
150 local String = tostring(Number) -- Used so we can manipulate the value in the last number placement
151
152 if Number >= 11 and Number <= 19 then -- Number is a teen, use 'th'
153 return String..'th'
154 elseif #String == 1 then -- Last digit is 1
155 return String..'st'
156 elseif #String == 2 then -- Last digit is 2
157 return String..'nd'
158 elseif #String == 3 then -- Last digit is 3
159 return String..'rd'
160 else
161 return String..'th'
162 end
163end
164
165
166
167-- Set presets
168local Presets = {
169 ['DecimalPlaces'] = 2
170}
171
172Module:SetSettings(Presets) -- Update presets table
173
174
175
176return Module