This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
This module extracts very basic numeric data from the input, namely the first match for a contiguous simple number, which may include the negative sign and a decimal, but not (yet) any further complexity, such as exponents, variables, etc.
Its primary function is accepting data like:
70%
margin-left: 20px;
75.485 Khz
and return the numeric portion of it so that it can be operated on arithmetically.Results for each string, respectively:
em
, px
, or %
to the bare-number proportions used by CSS's flex-grow
declaration (only works if the units on all the cells are the same; can't handle a mixture, e.g. of a fixed-width sidebar and relative-width main content area)."
and ;
characters and the like).cellspacing=...
to modern CSS td {padding: ...;}
on all sides...=..
character, use |1=
when calling the template. This never does any harm.a=70%
use {{#invoke:StripToNumbers|main|1=a=70%}}
→=
character unless it is escaped as {{=}}
or &equal;
.
=
that must be escaped.-
(the keyboard hyphen-minus character, not the formal unicode minus −
, and may contain a decimal; it throws away everything else.1.2.3
or 1-2-3
, nor null; this test may well be redundant code at this point, but better safe than sorry.main
function.=
-style character entities that are numeric (nor their hex equivalents), for obvious reasons, only named ones like &equal;nbsp;
This can be addressed in future upgrade, surely, but should be done in a separate function, as stripping such input down to the ASCII character numbers may well be the desired use in a particular instance.Basic usage:
{{#invoke:StripToNumbers | main | input }}
To divide the resulting value by two:
{{#invoke:StripToNumbers | halve | input }}
Same as main but returns null if no numbers in string, rather than error (can be used as contains numeric function):
{{#invoke:StripToNumbers | mainnull | input }}
local p = {}
function p.main(frame)
local theString = mw.text.unstrip(frame.args1])
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.args1])
local onlyNumber
onlyNumber = (string.match(theString, "%-?[%d%.]+"))
checkedNumber = tonumber(onlyNumber)
if checkedNumber == nil then
return nil
else
return checkedNumber
end
end
return p
This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
This module extracts very basic numeric data from the input, namely the first match for a contiguous simple number, which may include the negative sign and a decimal, but not (yet) any further complexity, such as exponents, variables, etc.
Its primary function is accepting data like:
70%
margin-left: 20px;
75.485 Khz
and return the numeric portion of it so that it can be operated on arithmetically.Results for each string, respectively:
em
, px
, or %
to the bare-number proportions used by CSS's flex-grow
declaration (only works if the units on all the cells are the same; can't handle a mixture, e.g. of a fixed-width sidebar and relative-width main content area)."
and ;
characters and the like).cellspacing=...
to modern CSS td {padding: ...;}
on all sides...=..
character, use |1=
when calling the template. This never does any harm.a=70%
use {{#invoke:StripToNumbers|main|1=a=70%}}
→=
character unless it is escaped as {{=}}
or &equal;
.
=
that must be escaped.-
(the keyboard hyphen-minus character, not the formal unicode minus −
, and may contain a decimal; it throws away everything else.1.2.3
or 1-2-3
, nor null; this test may well be redundant code at this point, but better safe than sorry.main
function.=
-style character entities that are numeric (nor their hex equivalents), for obvious reasons, only named ones like &equal;nbsp;
This can be addressed in future upgrade, surely, but should be done in a separate function, as stripping such input down to the ASCII character numbers may well be the desired use in a particular instance.Basic usage:
{{#invoke:StripToNumbers | main | input }}
To divide the resulting value by two:
{{#invoke:StripToNumbers | halve | input }}
Same as main but returns null if no numbers in string, rather than error (can be used as contains numeric function):
{{#invoke:StripToNumbers | mainnull | input }}
local p = {}
function p.main(frame)
local theString = mw.text.unstrip(frame.args1])
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.args1])
local onlyNumber
onlyNumber = (string.match(theString, "%-?[%d%.]+"))
checkedNumber = tonumber(onlyNumber)
if checkedNumber == nil then
return nil
else
return checkedNumber
end
end
return p