Module:Strip to numbers/doc
| This is a documentation subpage for Module:Strip to numbers. It may contain usage information, categories and other content that is not part of the original module page. |
| This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected. |
Usage
This module extracts numeric data from the input – very, very crudely.
Its primary function is accepting data like:
70%, ormargin-left: 20px;, or75.485 Khzand return the numeric portion of it so that it can be operated on arithmetically.
Results for each string:
- Result: 70
- Result: 20
- Result: 75.485
Use cases
- Converting layout table cell dimensions given in
em,px, or%to the bare-number proportions used by CSS'sflex-growdeclaration (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). - Converting sloppy template input generally (e.g. measurements with units attached when only the measurement is wanted).
- Auto-generating halved values, e.g. to translate between old-school HTML 4
cellspacing=...and modern CSStd {padding: ...;}
Limitations (serious ones)
At present it only does three things:
- It keeps numerals,
.characters, and-characters found in the input, and throws away everything else. - It checks that the result is a valid number (i.e. not something like
1.2.3or1-2-3) - It optionally divides the number by two.
Feel free to expand it to do more things and to do what it does more intelligently.
Presently it does not stop after finding a valid number. Thus, this string:
The values, respectively, for these fields in the case of Foo are: -12em, 34px, 56.7%, "89", 0
outputs this surely useless result:
- 1
Note that it dropped the 0 at the end of what it interpreted as a resulting decimal fraction, but that (to its credit) it was able to handle very simple wikimarkup in the input. A lot of stuff will make it choke, though.
It will also frequently choke on attempts to reduce entire CSS declarations to numbers, because they often contain multiple - characters; e.g. margin-left: -20px; will throw an error.
Note that it cannot handle &...; character entities that are numeric, for obvious reasons, only named ones like
Invocation
Basic usage:
{{#invoke:StripToNumbers | main | input }}
To divide the resulting value by two:
{{#invoke:StripToNumbers | halve | input }}
See also
- Module:ConvertNumeric - convert numbers to English words, and between number formats (e.g. decimal to hex)