Module:Sandbox/0xF8E8/yearsago

From English Wikipedia @ Freddythechick
functions = {}

function functions.nyearsago(frame)
	current_year = tonumber(os.date("%Y"))
	given_year = tonumber(frame.args[1])
	connector = false
	capitalize = false
	
	if frame.args[2] == "true" then connector = true end
    if frame.args[3] == "true" then capitalize = true end
    
	return "<span title=\"" .. given_year .. "\">" .. spelledout(current_year - given_year, connector, capitalize) .. " years ago</span>"
end

function functions.rand(frame)
	math.randomseed(os.time())
	num = math.random(1, tonumber(frame.args[1]))
	return num
end

function basicdigits(input_number)

		digits = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
		teens = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
		tens = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
		function oneto99(inp)
	    	if inp == 0 then
	    		return ""
			elseif inp < 10 then 
				return digits[inp + 1]
			elseif inp <= 90 and inp % 10 == 0 then
			    return tens[inp / 10 + 1] 
            elseif inp > 9 and inp < 20 then
			    return teens[inp - 9] 
			elseif inp > 19 and inp < 100 then
				hyphen = "-"
				if inp % 10 == 0 then
					hyphen = ""
				end
				return tens[(inp - (inp % 10))/10 + 1] .. hyphen .. digits[inp % 10 + 1] 
			end
	    end
	 
        if input_number < 100 then return oneto99(input_number)
        	
		elseif input_number > 99 and input_number < 1000 then
			return digits[(input_number - (input_number % 100)) / 100 +1] .. " hundred" .. connector .. oneto99(input_number % 100) 
		end
end

function spelledout(given_number, connect, capitalize) 
	connector = " "
	full_string = ""
 
    if tonumber(given_number) == 0 then return "zero" end
 
    if connect == true then 
		connector = " and "
	end
	
	suffixes = {"", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion"}
    
    j = 1
    
	for i=string.len(given_number), 1, -3 do
		strend = i - 2
		if strend < 1 then
			strend  = 1
		end
		spacing = ""
		if full_string ~= "" then spacing = " " end
	    full_string = basicdigits(tonumber(string.sub(given_number, strend, i))) .. " " .. suffixes[j] .. spacing .. full_string
	    j = j + 1
    end    
		
	if capitalize == true then
		return full_string:gsub("^%l", string.upper)
	else
		return full_string
	end
end

function functions.spelledout(frame)
	return spelledout(frame.args[1])
end

function functions.loopnum(frame)
	str = ""
	for i=1, tonumber(frame.args[1]) do
		str = str .. "\n\n" .. spelledout(i)
	end
	return str
end

return functions