Module:Number: Difference between revisions
(Created page with "local p = {} function p.to_hex( frame ) local num = tonumber(frame.args.num); if(num == nil) then return error("Provided argument (" .. frame.args.num .. "), cannot be converted to hexadecimal") end return num; end return p") |
(add format) |
||
| Line 1: | Line 1: | ||
--[[ | |||
Library for common number conversions. | |||
]] | |||
local p = {} | local p = {} | ||
function p.to_hex( frame ) | --probably worth revisiting this one and using string.format? | ||
function p.to_hex(frame) | |||
local num = tonumber(frame.args.num); | local num = tonumber(frame.args.num); | ||
if(num == nil) then return error("Provided argument (" .. frame.args.num .. "), cannot be converted to hexadecimal") end | if(num == nil) then | ||
return error("Provided argument (" .. frame.args.num .. "), cannot be converted to hexadecimal") | |||
end | |||
return num; | return num; | ||
end | |||
function p.format(frame) | |||
local format = "%.2f" | |||
local result | |||
if (frame.args.format ~= nil) then | |||
format = frame.args.format | |||
end | |||
result = string.format(format, frame.args.num) | |||
return result; | |||
end | end | ||
return p | return p | ||
Latest revision as of 17:25, 1 April 2024
Documentation for this module may be created at Module:Number/doc
--[[
Library for common number conversions.
]]
local p = {}
--probably worth revisiting this one and using string.format?
function p.to_hex(frame)
local num = tonumber(frame.args.num);
if(num == nil) then
return error("Provided argument (" .. frame.args.num .. "), cannot be converted to hexadecimal")
end
return num;
end
function p.format(frame)
local format = "%.2f"
local result
if (frame.args.format ~= nil) then
format = frame.args.format
end
result = string.format(format, frame.args.num)
return result;
end
return p