User:Σ/Testing facility/Rangemap
Appearance
Usage
[edit]{{rangemap | value = the number to map | from_low = the lower bound of the value's current range | from_high = the upper bound of the value's current range | to_low = the lower bound of the value's target range | to_high = the upper bound of the value's target range | precision = the number of decimals to round to }}
or, without specifying argument names:
{{rangemap|value|from_low|from_high|to_low|to_high}}
Description
[edit]Re-maps a number from one range to another. That is, a value of from_low would get mapped to to_low, a value of from_high to to_high, values in-between to values in-between, etc.
Does not constrain values to within the range, because out-of-range values are sometimes intended and useful.
Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so this template may be used to reverse a range of numbers, for example
{{rangemap|{{{x}}}|1|50|50|1}}
The template also handles negative numbers well, so that this example
{{rangemap|{{{x}}}|1|50|50|-100)
is also valid and works well.
Appendix
[edit]For the mathematically inclined, here's the expression:
(value - from_low) * (from_high - from_low) / (to_high - to_low) + to_low