Jump to content

Module:Strip to numbers/doc

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SMcCandlish (talk | contribs) at 09:26, 18 July 2015 (back to testing). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.


Usage

This module extracts numeric data from the input – very, very crudely.

Its primary function is accepting data like:

  • 70%, or margin-left: 20px;, or 75.485 Khz and 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'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).
  • 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 CSS td {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.3 or 1-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)