FANDOM


-- This module converts a number into its written French form.
-- For example, "2" becomes "deux", and "79" becomes "soixante-dix-neuf".
 
local getArgs = require('Module:Arguments').getArgs
 
local p = {}
 
local max = 100 -- The maximum number that can be parsed.
 
local ones = {
	[0] = 'zéro',
	[1] = 'un',
	[2] = 'deux',
	[3] = 'trois',
	[4] = 'quatre',
	[5] = 'cinq',
	[6] = 'six',
	[7] = 'sept',
	[8] = 'huit',
	[9] = 'neuf'
}
 
local specials = {
	[10] = 'dix',
	[11] = 'onze',
	[12] = 'douze',
	[13] = 'treize',
	[14] = 'quatorze',
	[15] = 'quinze',
	[16] = 'seize',
	[20] = 'vingt',
	[21] = 'vingt-et-un',
	[30] = 'trente',
	[31] = 'trente-et-un',
	[40] = 'quarante',
	[41] = 'quarante-et-un',
	[50] = 'cinquante',
	[51] = 'cinquante-et-un',
	[60] = 'soixante',
	[61] = 'soixante-et-un',
	[70] = 'soixante-dix',
	[71] = 'soixante-et-onze',
	[72] = 'soixante-douze',
	[73] = 'soixante-treize',
	[74] = 'soixante-quatorze',
	[75] = 'soixante-quinze',
	[76] = 'soixante-seize',
	[80] = 'quatre-vingt',
	[90] = 'quatre-vingt-dix',
	[91] = 'quatre-vingt-onze',
	[92] = 'quatre-vingt-douze',
	[93] = 'quatre-vingt-treize',
	[94] = 'quatre-vingt-quatorze',
	[95] = 'quatre-vingt-quinze',
	[96] = 'quatre-vingt-seize',
	[100] = 'cent'
}
 
local formatRules = {
	{num = 90, rule = 'quatre-vingt-dix-%s'},
	{num = 80, rule = 'quatre-vingt-%s'},
	{num = 70, rule = 'soixante-dix-%s'},
	{num = 60, rule = 'soixante-%s'},
	{num = 50, rule = 'cinquante-%s'},
	{num = 40, rule = 'quarante-%s'},
	{num = 30, rule = 'trente-%s'},
	{num = 20, rule = 'vingt-%s'},
	{num = 10, rule = 'dix-%s'}
}
 
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
Sauf mention contraire, le contenu de la communauté est disponible sous licence CC-BY-SA .