Module:NumberSpell
-- This module converts a number into its written English form.
-- For example, "2" becomes "two", and "79" becomes "seventy-nine".
local getArgs = require('Module:Arguments').getArgs
local p = {}
local max = 100 -- The maximum number that can be parsed.
local ones = {
[0] = 'zero',
[1] = 'ane',
[2] = 'twa',
[3] = 'three',
[4] = 'fower',
[5] = 'five',
[6] = 'sax',
[7] = 'seiven',
[8] = 'aicht',
[9] = 'nine'
}
local specials = {
[10] = 'ten',
[11] = 'eleiven',
[12] = 'twal',
[13] = 'thirteen',
[15] = 'fifteen',
[18] = 'aichteen',
[20] = 'twenty',
[30] = 'thirty',
[40] = 'fowerty',
[50] = 'fifty',
[60] = 'saxty',
[70] = 'seiventy',
[80] = 'aichty',
[90] = 'ninety',
[100] = 'ane hunder'
}
local formatRules = {
{num = 90, rule = 'ninety-%s'},
{num = 80, rule = 'aichty-%s'},
{num = 70, rule = 'seiventy-%s'},
{num = 60, rule = 'saxty-%s'},
{num = 50, rule = 'fifty-%s'},
{num = 40, rule = 'fowerty-%s'},
{num = 30, rule = 'thirty-%s'},
{num = 20, rule = 'twenty-%s'},
{num = 10, rule = '%steen'}
}
function p.main(frame)
local args = getArgs(frame)
local num = tonumber(args[1])
local success, result = pcall(p._main, num)
if success then
return result
else
return string.format('<strong class="error">Error: %s</strong>', result) -- "result" is the error message.
end
return p._main(num)
end
function p._main(num)
if type(num) ~= 'number' or math.floor(num) ~= num or num < 0 or num > max then
error('input must be an integer between 0 and ' .. tostring(max), 2)
end
-- Check for numbers from 0 to 9.
local onesVal = ones[num]
if onesVal then
return onesVal
end
-- Check for special numbers.
local specialVal = specials[num]
if specialVal then
return specialVal
end
-- Construct the number from its format rule.
onesVal = ones[num % 10]
if not onesVal then
error('Unexpected error parsing input ' .. tostring(num))
end
for i, t in ipairs(formatRules) do
if num >= t.num then
return string.format(t.rule, onesVal)
end
end
error('No format rule found for input ' .. tostring(num))
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.