Module:Utility

--[=[

LUA Module for Utility Functions

wordinal: Word-Ordinal form of number Parameters: 1 = Number (0-99) Return: Ordinal in Word form of number "??" for 0 or non-numeric input Input number for number >= 100

ordinal: Ordinal Numbers Parameters: 1 = Number 2 = Cardinal (return cardinal number if true) Return: Ordinal Number (Or simply number if cardinal is true) it does not seem logical to have an ordinal function return cardinal numbers, but it saves 'Ifs' in the calling module --]=]

local p = {}

local tOnesOrd = { [0] = '??',	[1] = 'first', [2] = 'second', [3] = 'third', [4] = 'fourth', [5] = 'fifth', [6] = 'sixth', [7] = 'seventh', [8] = 'eighth', [9] = 'ninth', [10] = 'tenth', [11] = 'eleventh', [12] = 'twelfth', [13] = 'thirteenth', [14] = 'fourteenth', [15] = 'fifteenth', [16] = 'sixteenth', [17] = 'seventeenth', [18] = 'eighteenth', [19] = 'nineteenth' }

local tTensOrd = { [2] = 'twentieth', [3] = 'thirtieth', [4] = 'fortieth', [5] = 'fiftieth', [6] = 'sixtieth', [7] = 'seventieth', [8] = 'eightieth', [9] = 'ninetieth' }

local tTens = { [2] = 'twenty', [3] = 'thirty', [4] = 'forty', [5] = 'fifty', [6] = 'sixty', [7] = 'seventy', [8] = 'eighty', [9] = 'ninety' }

-- return word form ordinal number function p.wordinal(num) local n = tonumber(num) or 0 if n >= 100 then return num end

if n < 20 then return tOnesOrd[n] elseif n % 10 == 0 then return tTensOrd[n / 10] else return tTens[math.floor(n / 10)] .. '-' .. tOnesOrd[n % 10] end end

-- return ordinal number function p.ordinal(num, cardinal) local n = tonumber(num) or 0 local i = math.fmod(n,10) local o = ""

if not cardinal then o = (i==1 and n~=11 and "st") or (i==2 and n~=12 and "nd") or (i==3 and n~=13 and "rd") or (n~=0 and "th") or "" end

return tostring(n) .. o end

return p