-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.lua
67 lines (54 loc) · 1.87 KB
/
Functions.lua
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
function RoundInteger(number) --Rounds input to nearest whole number
return math.floor(number+0.5)
end
--Example: RoundInteger(11.87) will return 12
function RoundFactor(number,factor) --Rounds number to nearest multiple of factor
return math.floor(number/factor+0.5)*factor
end
--Example: RoundFactor(563,100) will return 600
function CommaSeparator(number) --Inserts commas every third digit
return tostring(number):reverse():gsub("%d%d%d","%1,"):reverse():gsub("^,", "")
end
--Example: CommaSeparator(525724) will return 525,724
function SpaceSeparator(number) --Inserts spaces every third digit
return tostring(number):reverse():gsub("%d%d%d","%1 "):reverse():gsub("^,", "")
end
--Example: SpaceSeparator(482583) will return 482 583
function toboolean(object) --Takes a string or integer value and returns a boolean
if object==1 or object=="1" or object=="true" then return true end
if object==0 or object=="0" or object=="false" then return false end
end
--Example: toboolean(0) will return false
--Example: toboolean("1") will return true
function TablePrint(table)
if type(table) == 'table' then
local s = '{ '
for k,v in pairs(table) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. TablePrint(v) .. ','
end
return s .. '} '
else
return tostring(table)
end
end
function Capitalize(string)
string:gsub("%f[%a].", string.upper)
return string
end
--Example: Capitalize("testing this function") will return "Testing This Function"
function StringToTable()
SampleString = ""
SampleTable = {}
for k,v in string.gmatch(SampleString,"(%w+)=(%w+)") do
SampleTable[k] = v
end
end
function TableToString()
SampleTable = {}
SampleString = ""
for k,v in pairs(SampleTable) do
if string.len(SampleString)>0 then c = "," else c = "" end
SampleString = SampleString..c..k.."="..v
end
end