Module:ConvertMaxWidth: Difference between revisions
Appearance
Created page with "local p = {} function p.main(frame) local args = frame.args -- Get input value (yards) local yards = tonumber(args[1]) if not yards then return "Error: Invalid or missing yard value." end -- Decimal precision (default: 1) local precision = tonumber(args['precision']) or 1 -- Abbreviation toggle local abbr = args['abbr'] or 'on' -- Conversions local miles = yards / 1760 local km = yards * 0.0009144 -- Forma..." |
No edit summary |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local function addCommas(n) | |||
local s = tostring(n) | |||
while true do | |||
s, k = s:gsub("^(-?%d+)(%d%d%d)", "%1,%2") | |||
if k == 0 then break end | |||
end | |||
return s | |||
end | |||
local function roundSigFig(num, sigfig) | |||
if num == 0 then return 0 end | |||
local scale = 10 ^ (sigfig - math.floor(math.log10(math.abs(num))) - 1) | |||
return math.floor(num * scale + 0.5) / scale | |||
end | |||
function p.main(frame) | function p.main(frame) | ||
local args = frame.args | local args = frame.args | ||
local yards = tonumber(args[1]) | local yards = tonumber(args[1]) | ||
if not yards then | if not yards then | ||
| Line 10: | Line 24: | ||
end | end | ||
-- | -- Default to 3 significant figures (matches Wikipedia) | ||
local | local sigfig = tonumber(args['sigfig']) or 3 | ||
local abbr = args['abbr'] or 'on' | local abbr = args['abbr'] or 'on' | ||
-- Conversions | -- Conversions (exact) | ||
local miles = yards / 1760 | local miles = yards / 1760 | ||
local km = yards * 0.0009144 | local km = yards * 0.0009144 | ||
-- | -- Sig-fig rounding | ||
local formattedYards = | local milesR = roundSigFig(miles, sigfig) | ||
local formattedMiles = | local kmR = roundSigFig(km, sigfig) | ||
local formattedKm = | |||
local formattedYards = addCommas(math.floor(yards)) | |||
local formattedMiles = tostring(milesR) | |||
local formattedKm = tostring(kmR) | |||
if abbr == 'on' then | if abbr == 'on' then | ||
return formattedYards .. " yards (" .. | |||
formattedMiles .. " mi; " .. | |||
formattedKm .. " km)" | |||
else | |||
return formattedYards .. " yards (" .. | |||
formattedMiles .. " miles; " .. | |||
formattedKm .. " kilometers)" | |||
end | |||
end | end | ||
return p | return p | ||
Latest revision as of 19:33, 3 January 2026
Documentation for this module may be created at Module:ConvertMaxWidth/doc
local p = {}
local function addCommas(n)
local s = tostring(n)
while true do
s, k = s:gsub("^(-?%d+)(%d%d%d)", "%1,%2")
if k == 0 then break end
end
return s
end
local function roundSigFig(num, sigfig)
if num == 0 then return 0 end
local scale = 10 ^ (sigfig - math.floor(math.log10(math.abs(num))) - 1)
return math.floor(num * scale + 0.5) / scale
end
function p.main(frame)
local args = frame.args
local yards = tonumber(args[1])
if not yards then
return "Error: Invalid or missing yard value."
end
-- Default to 3 significant figures (matches Wikipedia)
local sigfig = tonumber(args['sigfig']) or 3
local abbr = args['abbr'] or 'on'
-- Conversions (exact)
local miles = yards / 1760
local km = yards * 0.0009144
-- Sig-fig rounding
local milesR = roundSigFig(miles, sigfig)
local kmR = roundSigFig(km, sigfig)
local formattedYards = addCommas(math.floor(yards))
local formattedMiles = tostring(milesR)
local formattedKm = tostring(kmR)
if abbr == 'on' then
return formattedYards .. " yards (" ..
formattedMiles .. " mi; " ..
formattedKm .. " km)"
else
return formattedYards .. " yards (" ..
formattedMiles .. " miles; " ..
formattedKm .. " kilometers)"
end
end
return p