Nieuwe pagina aangemaakt met 'local p = {} function p.main(frame) local theString = mw.text.unstrip(frame.args[1]) local onlyNumber onlyNumber = (string.match(theString, "%-?[%d%.]+")) check...' |
Geen bewerkingssamenvatting |
||
| Regel 1: | Regel 1: | ||
local p = {} | local p = {} | ||
function p.main(frame) | function p.main(frame) | ||
local theString = mw.text.unstrip(frame.args[1]) | local theString = mw.text.unstrip(frame.args[1]) | ||
| Regel 18: | Regel 19: | ||
return halvedNumber | return halvedNumber | ||
end | end | ||
function p.mainnull(frame) | function p.mainnull(frame) | ||
local theString = mw.text.unstrip(frame.args[1]) | local theString = mw.text.unstrip(frame.args[1]) | ||
local fallback = mw.text.unstrip(frame.args[2]) | |||
local onlyNumber | local onlyNumber | ||
onlyNumber = (string.match(theString, "%-?[%d%.]+")) | onlyNumber = (string.match(theString, "%-?[%d%.]+")) | ||
checkedNumber = tonumber(onlyNumber) | checkedNumber = tonumber(onlyNumber) | ||
if checkedNumber == nil then | if checkedNumber == nil then | ||
return | return fallback | ||
else | else | ||
return checkedNumber | return checkedNumber | ||
end | end | ||
end | end | ||
return p | return p | ||
Versie van 25 jan 2023 19:40
Module:StripToNumbers
Summary
Example: fetch first number:
"sdfsdf 23.56 sdfsdf 33" (using main) => 23.56
"text without number" (using mainnull, with fallback text) => No number foundlocal p = {}
function p.main(frame)
local theString = mw.text.unstrip(frame.args[1])
local onlyNumber
onlyNumber = (string.match(theString, "%-?[%d%.]+"))
checkedNumber = tonumber(onlyNumber)
if checkedNumber == nil then
error(" Input did not contain valid numeric data")
else
return checkedNumber
end
end
function p.halve(frame)
local checkedNumber = (p.main(frame))
local halvedNumber
halvedNumber = (checkedNumber / 2)
return halvedNumber
end
function p.mainnull(frame)
local theString = mw.text.unstrip(frame.args[1])
local fallback = mw.text.unstrip(frame.args[2])
local onlyNumber
onlyNumber = (string.match(theString, "%-?[%d%.]+"))
checkedNumber = tonumber(onlyNumber)
if checkedNumber == nil then
return fallback
else
return checkedNumber
end
end
return p
