Module:ConvertNumeric
Appearance
![]() | This Lua module is used on approximately 19,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
![]() | This Lua module is used in MediaWiki:Watchlist-messages, and on approximately 19,000 pages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
Usage
{{#invoke:ConvertNumeric|function_name}}
See also
- {{Spellnum per MOS}}
- {{Number to word}}
- Module:Strip to numbers – extract a number from a string (supports negatives and decimals) and return it, or optionally return a halved value
-- Module for converting between different representations of numbers.
local ones_position = {
[0] = 'zero',
[1] = 'one',
[2] = 'two',
[3] = 'three',
[4] = 'four',
[5] = 'five',
[6] = 'six',
[7] = 'seven',
[8] = 'eight',
[9] = 'nine',
[10] = 'ten',
[11] = 'eleven',
[12] = 'twelve',
[13] = 'thirteen',
[14] = 'fourteen',
[15] = 'fifteen',
[16] = 'sixteen',
[17] = 'seventeen',
[18] = 'eighteen',
[19] = 'nineteen',
}
local tens_position = {
[2] = 'twenty',
[3] = 'thirty',
[4] = 'forty',
[5] = 'fifty',
[6] = 'sixty',
[7] = 'seventy',
[8] = 'eighty',
[9] = 'ninety'
}
function numeral_to_english_less_100(num)
if num < 20 then
return ones_position[num]
elseif num % 10 == 0 then
return tens_position[num / 10]
else
return tens_position[math.floor(num / 10)] .. '-' .. ones_position[num % 10]
end
end
function numeral_to_english_less_1000(num, use_and)
if num < 100 then
return numeral_to_english_less_100(num)
elseif num % 100 == 0 then
return ones_position[num/100] .. ' hundred'
else
local s = ones_position[math.floor(num/100)] .. ' hundred '
if use_and then s = s .. 'and ' end
s = s .. numeral_to_english_less_100(num % 100)
return s
end
end
local p = {}
function p._numeral_to_english(num, capitalize)
local s = ''
if num >= 1000000000 then
s = s .. numeral_to_english_less_1000(math.floor(num/1000000000), false) .. ' billion'
num = num % 1000000000
end
if num >= 1000000 then
if s ~= '' then s = s .. ', ' end
s = s .. numeral_to_english_less_1000(math.floor(num/1000000), false) .. ' million'
num = num % 1000000
end
if num >= 1000 then
if s ~= '' then s = s .. ', ' end
s = s .. numeral_to_english_less_1000(math.floor(num/1000), false) .. ' thousand'
num = num % 1000
end
if s ~= '' and num > 0 then
if num >= 100 then
s = s .. ', '
else
s = s .. ' and '
end
end
if s == '' or num > 0 then
s = s .. numeral_to_english_less_1000(math.floor(num), true)
end
s = s:gsub("^%s*(.-)%s*$", "%1") -- Trim whitespace
if capitalize then s = s:gsub("^%l", string.upper) end
return s
end
function p.numeral_to_english(frame)
local num = tonumber(frame.args[1])
local capitalize = frame.args['capitalize']
return _numeral_to_english(num, capitalize)
end
return p