Module:Convert: Difference between revisions
Jump to navigation
Jump to search
>WOSlinker No edit summary |
>Uncle G (Eliminated a common subexpression.) |
||
Line 10: | Line 10: | ||
function convert(in_val,in_unit,out_unit) | function convert(in_val,in_unit,out_unit) | ||
local out_val; | local out_val; | ||
local units = in_unit .. "|" .. out_unit | |||
if units == "°C|°F" or units == "C|F" then | |||
out_val = in_val * 9 / 5 + 32; | out_val = in_val * 9 / 5 + 32; | ||
elseif | elseif units == "°F|°C" or units == "F|C" then | ||
out_val = (in_val - 32) * 5 / 9; | out_val = (in_val - 32) * 5 / 9; | ||
elseif | elseif units == "kg|lb" then | ||
out_val = in_val * 2.20462262; | out_val = in_val * 2.20462262; | ||
elseif units == "lb|kg" then | |||
out_val = in_val / 2.20462262; | |||
else | else | ||
error ("Module:Converting between " .. in_unit .. " and " .. out_unit .. " not supported."); | error ("Module:Converting between " .. in_unit .. " and " .. out_unit .. " not supported."); |
Revision as of 08:49, 4 September 2012
Documentation for this module may be created at Module:Convert/doc
--require "mw.text" --require "mw.page" -- TODO: lots of things still need doing but below are a few of the major bits -- Support for numeric precision -- Support for all the different conversion units local p = {} function convert(in_val,in_unit,out_unit) local out_val; local units = in_unit .. "|" .. out_unit if units == "°C|°F" or units == "C|F" then out_val = in_val * 9 / 5 + 32; elseif units == "°F|°C" or units == "F|C" then out_val = (in_val - 32) * 5 / 9; elseif units == "kg|lb" then out_val = in_val * 2.20462262; elseif units == "lb|kg" then out_val = in_val / 2.20462262; else error ("Module:Converting between " .. in_unit .. " and " .. out_unit .. " not supported."); end return out_val end -- This is the top-level function called by {{convert}}. function p.main(frame, config, args) local pframe = frame:getParent(); local args = pframe.args; -- the arguments passed TO the {{convert}} template, in the wikitext that instantiates the template local config = frame.args; -- the arguments passed BY the {{convert}} template, in the wikitext of the template itself local output; in_val1 = tonumber(args[1]); in_val2 = tonumber(args[3]); disp = args["disp"]; if in_val1 == nil then error ("Module:Convert value not supplied"); return "" end if in_val2 == nil then -- Single value supplied in_unit = args[2]; out_unit = args[3]; out_val1 = convert(in_val1,in_unit,out_unit); if disp == "or" then output = in_val1 .. " " .. in_unit .. " or " .. out_val1 .. " " .. out_unit; elseif disp == "sqbr" then output = in_val1 .. " " .. in_unit .. " <nowiki>[</nowiki>" .. out_val1 .. " " .. out_unit .. "<nowiki>]</nowiki>"; else output = in_val1 .. " " .. in_unit .. " (" .. out_val1 .. " " .. out_unit .. ")"; end else -- Two values supplied range = args[2]; in_unit = args[4]; out_unit = args[5]; out_val1 = convert(in_val1,in_unit,out_unit); out_val2 = convert(in_val2,in_unit,out_unit); output = in_val1 .. " " .. range .. " " .. in_val2 .. " " .. in_unit .. " (" .. out_val1 .. " " .. range .. " " .. out_val2 .. " " .. out_unit .. ")"; end --error ("Module:Convert is not implemented"); return output end return p