Modul:Measure
Modul:Measure
| Vorlagenprogrammierung | Diskussionen | Lua | Test | Unterseiten | |||
| Modul | Deutsch | English
|
Modul: | Dokumentation | |||
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
local NoError, FormatNum = pcall( require, "Module:FormatNum" )
if type( FormatNum ) == "table" then
FormatNum = FormatNum.FormatNum()
else
return '<span class="error"></span>'
end
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
-- AutoConvert: Wie Convert, jedoch mit den Schwellwerten 1 und factorU
Measure.AutoConvert = function (tArgs)
local tResult = {}
local IsOk = true;
if type(tArgs.factorU or 0) ~= 'number' then
return false, tResult;
end
tArgs.borderL = 1; -- andere Werte werden hier einfach überschrieben.
tArgs.borderU = tArgs.factorU; -- andere Werte werden hier einfach überschrieben.
IsOk, tResult = Measure.Convert(tArgs)
return IsOk, 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
-- Run enthält die Gemeinsamkeiten der Funktionen p.Mass und p.Masz
local function Run (frame,fmt)
local Text = " "
local tData = {}
local IsOk = true
local tbl = {}
local Para = "";
if tonumber(frame.args[1]) then
tbl.data = tonumber(frame.args[1]);
else
Para = "(".. (frame.args[1] or "") .. ")";
Para = frame:callParserFunction{ name = '#expr', args = Para }
tbl.data = tonumber(Para) or "";
end
tbl.unit = frame.args[2] or "";
tbl.borderL = tonumber(frame.args[3] or "")
tbl.unitL = frame.args[4] or "";
tbl.factorL = tonumber(frame.args[5] or 0)
tbl.borderU = tonumber(frame.args[6] or "")
tbl.unitU = frame.args[7] or "";
tbl.factorU = tonumber(frame.args[8] or 0)
tbl.eps = tonumber(frame.args.eps or "")
tbl.round = tonumber(frame.args.round 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;
if tbl.data <10000 then
fmt="de";
end
end
if tbl.unit =="km²" and tbl.unitL=="" then
tbl.borderL = 1;
tbl.unitL = "ha";
tbl.factorL = 100;
if tbl.data <10000 then
fmt="de";
end
end
if tbl.unit =="" then
tbl.borderL = 1;
tbl.factorL = 0;
end
IsOk, tData = Measure.Convert(tbl)
tData.data = frame:expandTemplate{title = 'FormatNum', args = {tData.data,fmt}}
--tData.data = FormatNum.format(tData.data,fmt)
if tonumber(tData.eps) then
tData.eps = frame:expandTemplate{title = 'FormatNum', args = {tData.eps, fmt}}
-- tData.eps = FormatNum.format(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 -- Run
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
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.