Module:NumberToText

From Inkipedia, the Splatoon wiki
Revision as of 09:40, 30 January 2024 by Exaskliri (talk | contribs) (moved tonumber conversion to args function)

Spells a given number in English.

Usage

{{NumberToText|<number>}}

number Unnamed, required Number to be converted to text.

local p = {}

function p.textFromNumber(args)
	local oneslist = { [0]="", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }
	local teenlist = { [0]="ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }
	local tenslist = { [0]="", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }
	local lionlist = { [0]="", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion" }
	local abs, floor = math.abs, math.floor
	
	if (args[1] == 0) then return "zero" end
	local absnum, lion, result = abs(args[1]), 0, ""
	local function dashed(s) return s=="" and s or "-"..s end
	local function spaced(s) return s=="" and s or " "..s end
	while (absnum > 0) do
		local word, ones, tens, huns = "", absnum%10, floor(absnum/10)%10, floor(absnum/100)%10
		if (tens==0) then word = oneslist[ones]
		elseif (tens==1) then word = teenlist[ones]
		else word = tenslist[tens] .. dashed(oneslist[ones]) end
		if (huns > 0) then word = oneslist[huns] .. " hundred" .. spaced(word) end
		if (word ~= "") then result = word .. spaced(lionlist[lion]) .. spaced(result) end
		absnum = floor(absnum / 1000)
		lion = lion + 1
	end
	if (args[1] < 0) then result = "minus " .. result end
	return result
end

function p.main(frame)
	local args = frame:getParent().args
	args[1] = tonumber(args[1], args[2])
	return p.textFromNumber(args)
end

return p
-- Thank https://rosettacode.org/wiki/Number_names#Lua