Module:Maß
Appearance
Module created by Antonsusi (talk) Tests for the German Wikipedia.
Used pages:
- This module
- Template:Maß
- Template:Mass
local Measure = {}
Measure.Convert = function (tArgs)
--[=[ enthält die Umrechnung
Inhalt von tArgs:
tArgs.data number Zu konvertierender Wert
tArgs.unit string Vorgabeeinheit von data
tArgs.unitL string etwaige, kleitere Einheit
tArgs.unitU string etwaige, größere Einheit
tArgs.factorL number Faktor unit/unitL, Null für keine Umrechnung
tArgs.factorU number Faktor unitU/unit, Null für keine Umrechnung
tArgs.borderL number Schwellwert zur kleineren Einheit
tArgs.borderU number Schwellwert zur größeren Einheit
tArgs.eps number Unsicherheit false für kein Wert
tArgs.round number Nachkommastellen
]=]
tResult = {}
if type(tArgs.data) ~= 'number' or
type(tArgs.borderL or 0) ~= 'number' or
type(tArgs.factorL or 0) ~= 'number' or
type(tArgs.borderU or 0) ~= 'number' or
type(tArgs.factorU or 0) ~= 'number' or
(tArgs.unit or "") =="" then
return false, tResult;
end
local IsMinus = tArgs.data < 0;
if IsMinus then
tArgs.data = 0 - tArgs.data
end
tResult.data = tArgs.data
tResult.unit = tArgs.unit
if tonumber(tArgs.eps) then
tResult.eps = tArgs.eps
end
if (tArgs.factorL or 0) ~= 0 then
if tArgs.data < tArgs.borderL then
tResult.data = tArgs.data * tArgs.factorL
if type(tArgs.eps) == 'number' then
tResult.eps = tArgs.eps * tArgs.factorL
end
tResult.unit = tArgs.unitL
end
end
if (tArgs.factorU or 0) ~= 0 then
if tArgs.data >= tArgs.borderU then
tResult.data = tArgs.data / tArgs.factorU
if type(tArgs.eps) == 'number' then
tResult.eps = tArgs.eps / tArgs.factorU
end
tResult.unit = tArgs.unitU
end
end
if IsMinus then
tResult.data = 0 - tResult.data
end
if type(tArgs.round) == 'number' then
tArgs.round = math.floor(tArgs.round);
tResult.data = math.floor(tResult.data * 10^tArgs.round + 0.5)/10^tArgs.round ;
end
return true, tResult;
end
Measure.Format = function (tArgs,tPara)
--[=[ enthält die Formatierung der Zeichenkette
Inhalt von tArgs:
tArgs.data string Wert
tArgs.unit string Einheit
tArgs.eps string Unsicherheit
Inhalt von tPara:
tPara.fmt string Zahlenformat: 'dewiki' oder 'ch'
tPara.kl logical true -> '(10 ± 2) km', false -> '10 ± 2 km'
]=]
local Text = ""
if (tArgs.eps or "") ~= "" then
if tPara.kl then
Text = "(" .. tostring(tArgs.data) .. " ± " .. tostring(tArgs.eps) .. ")"
else
Text = tArgs.data .. " ± " .. tostring(tArgs.eps)
end
else
Text = tArgs.data
end
Text = Text .. " " .. tArgs.unit;
return Text
end
local function Run (frame,fmt)
local Text = ""
local tData = {}
local IsOk = true
local tbl = {}
tbl.unitU = "";
tbl.factorU = 0;
tbl.borderU = 0;
tbl.unit = frame.args[2] or "";
tbl.unitL = frame.args[4] or "";
tbl.data = tonumber(frame.args[1] or "")
-- local Para = "(".. (frame.args[1] or "") .. ")";
-- Para = frame:callParserFunction{ name = '#expr', args = Para }
-- tbl.data = tonumber(Para);
tbl.factorL = tonumber(frame.args[5] or 0)
tbl.borderL = tonumber(frame.args[3] or "")
tbl.eps = tonumber(frame.args.eps or "")
tbl.round = tonumber(frame.args[6] or "")
if type(tbl.data) ~= 'number' then
Text = tostring(frame.args[1] or "") .. '<span style="display:none">[[Vorlage:Maß/Wartung/Maßzahl nicht numerisch|dep1]]</span>';
return false, Text
end
-- Sonderregelungen km und km²
if tbl.unit =="km" and tbl.unitL=="" then
tbl.borderL = 1;
tbl.unitL = "m";
tbl.factorL = 1000;
end
if tbl.unit =="km²" and tbl.unitL=="" then
tbl.borderL = 1;
tbl.unitL = "ha";
tbl.factorL = 100;
end
IsOk, tData = Measure.Convert(tbl)
tData.data = frame:expandTemplate{title = 'FormatNum', args = {tData.data,fmt}}
if tonumber(tData.eps) then
tData.eps = frame:expandTemplate{title = 'FormatNum', args = {tData.eps, fmt}}
end
local tPar = {}
tPar.kl = (frame.args.kl or "") ~= ""
tPar.fmt = fmt;
if not IsOk then
Text = '<span class="error">Unzulässige Werte bei Aufruf von Measure;</span>';
return false, Text
end
Text = Measure.Format(tData,tPar);
return true, Text
end
local p = {}
function p.Masz(frame)
local FR = frame:getParent()
local IsOk, Out = Run(FR,'dewiki')
return Out
end
function p.Mass(frame)
local FR = frame:getParent()
local IsOk, Out = Run(FR,'ch')
return Out
end
function p.Measure()
return Measure
end
return p