Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Roman: Difference between revisions

From Neodyland Wiki
Add floor calls
m debug
Line 126: Line 126:
local output = ''
local output = ''
return thousands[1]
--[[
if ((value >= 0) and (value < 5000)) then
if ((value >= 0) and (value < 5000)) then
index = (floor(value / 1000) % 5) + 1
index = (floor(value / 1000) % 5) + 1
Line 136: Line 138:
output = output .. ones[index]
output = output .. ones[index]
end
end
 
return output
return output
]]
end
end



Revision as of 04:45, 27 December 2014

Module documentation[ view · edit · history · purge ]
This documentation is transcluded from Module:Roman/doc.

Summary

This module contains functions for working with Roman numerals. Currently used by:

Using this module from templates

Numeral

This function converts an Arabic numeral into a Roman numeral. It works for values between 0 (N) and 4999999999 (M̿M̿M̿M̿C̿M̿X̿C̿I̿X̿C̅M̅X̅C̅I̅X̅CMXCIX): this includes the whole range of unsigned 32-bit integers. The output string no longer contain HTML tags. If needed you can add external CSS formatting using a serif font family, or a small-caps font variant. Arabic numeral zero is output as 'N' (for Classical Latin adverbs "Nec" or "non"), like in standard CLDR Data.

If the input does not look like it contains a number or the number is outside of the supported range, an error message is returned. If an error message is returned, the error message will contain code to categorize pages into Category:Errors reported by Module Roman.

Usage:

{{#invoke:Roman|Numeral|''value''}}

Example: {{#invoke:Roman|Numeral|8}} produces .

Arabic

This function converts a Roman numeral into an Arabic numeral. It works for values between 0 (N) and 4999999999 (M̿M̿M̿M̿C̿M̿X̿C̿I̿X̿C̅M̅X̅C̅I̅X̅CMXCIX): this includes the whole range of unsigned 32-bit integers.

If the input does not look like it contains a number or the number is outside of the supported range, an error message is returned. If an error message is returned, the error message will contain code to categorize pages into Category:Errors reported by Module Roman.

Usage:

{{#invoke:Roman|Arabic|''value''}}

Example: {{#invoke:Roman|Arabic|viii}} produces Script error: The function "Arabic" does not exist..

Using this module from Lua code

In order to use the functions in this module from another Lua module you first have to import this module.

Example:

local roman = require('Module:Roman')

_Numeral

This function converts an Arabic numeral into a Roman numeral. It works for values between 0 and 4999999. The output string may contain HTML tags. Arabic numeral zero is output as an empty string. If the input does not look like it contains a number or the number is outside of the supported range an error message is returned. If an error message is returned, the error message will contain code to categorize pages into Category:Errors reported by Module Roman.

Usage:

roman_value = roman._Numeral(value)

isRoman

Tests if the input is a valid Roman numeral. Returns true if so, false if not. For the purposes of this function, the empty string is not a valid Roman numeral.

Usage:

if roman.isRoman(roman_value) then

toArabic

This function converts a Roman numeral into an Arabic numeral. It works for values between 0 and 4999999999. The string 'N' is converted to zero. If the input is not a valid Roman numeral this function attempts to parse it as an Arabic number and returns nil if it also fails.

Usage:

arabic_value = roman.toArabic(value)
--[[  
 
This module converts Arabic numerals i nto Roman numerals.  It currently works for any 
whole number between 1 and 4999999.
 
Please do not modify this code without applying the changes first at Module:Roman/sandbox and testing 
at Module:Roman/sandbox/testcases and Module talk:Roman/sandbox/testcases.
 
Authors and maintainers:
* User:RP88
 
]]

local p = {}
 
--[[
Year
 
This function returns a year formatted as a Roman numeral.  It works for years between 
1 and 4999999.  The output string may contain HTML tags.
 
Usage:
{{#invoke:Roman|Year|<year>}}
 
Parameters
    1: Year to display in Roman numerals. Must be at least 1 and less than 5,000,000. 
 
Error Handling:
   If the input does not look like it contains a year or the year is outside of the
   supported range an error message is returned.
]]
function p.Year( frame )
	local output = ''

	local args = frame.args
	-- if no argument provided than check parent template/module args
	if args[1]==nil then
		args = frame:getParent().args 
		-- if no argument in parent as well, then output current year in Roman numerals
		if args[1]==nil then
			return frame:callParserFunction( "#time", 'xrY' )
		end
	end
	
	local input = args[1]
	if input then
		local value = tonumber(input)
		if value and (value > 0) and (value < 5000000) then
			output = p._HTMLNumeral( value )
		else
			output = p._error( "unsupported value for year" )
		end
	else
		output = p._error( "missing value for year" )
	end
		
	return output
end


--[[
Numeral
 
This function converts an Arabic numeral into a Roman numeral.  It works for values between 
0 and 4999999.  The output string may contain HTML tags.  Arabic numeral zero is output as
an empty string.
 
Usage:
{{#invoke:Roman|Numeral|<value>}}
 
Parameters
    1: Value to convert into a Roman numeral. Must be at least 0 and less than 5,000,000. 
 
Error Handling:
   If the input does not look like it contains a number or the number is outside of the
   supported range an error message is returned.
]]
function p.Numeral( frame )
	local input = frame.args[1]
	local output = ''
	
	if input then
		local value = tonumber(input)
		if value and (value >= 0) and (value < 5000000) then
			output = p._HTMLNumeral( value )
		else
			output = p._error( "unsupported value" )
		end
	else
		output = p._error( "missing value" )
	end
		
	return output
end


--[[
This function returns a string containing the input value formatted as a Roman numeral.  It works for values between 
0 and 4999999. The output string may contain HTML tags.
]]
function p._HTMLNumeral ( value )
	local output = ''
	
	if (value < 5000) then
		output = p._Numeral ( value )
	else
		local low_value = floor(value) % 5000;
		local high_value = (value - low_value) / 1000
		output = '<span style="text-decoration:overline;">' .. p._Numeral( high_value ) .. _ '</span>' .. p._Numeral( low_value )
	end
	
	return output
end


--[[
This function returns a string containing the input value formatted as a Roman numeral.  It works for values between 
0 and 4999. The output string will be a simple alphanumeric string.
]]
function p._Numeral ( value )
	local thousands = {'', 'M', 'MM', 'MMM', 'MMMM'}
	local hundreds = {'', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'}
	local tens = {'', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'}
	local ones = {'', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'}
	local index
	local output = ''
	
	return thousands[1]
	--[[
	if ((value >= 0) and (value < 5000)) then
		index = (floor(value / 1000) % 5) + 1
		output = output .. thousands[index]
		index = (floor(value / 100) % 10) + 1
		output = output .. hundreds[index]
		index = (floor(value / 10) % 10) + 1
		output = output .. tens[index]
		index = (floor(value) % 10) + 1
		output = output .. ones[index]
	end

	return output
	]]
end


--[[
Helper function to handle error messages.
]]
function p._error( error_str )
    local error_str = '<strong class="error">Roman Module Error: ' .. error_str .. '</strong>';
    error_str = '[[Category:Errors reported by Module Roman]]' .. error_str;
 
    return error_str;
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.