# Map a value from one number scale to another – formula and C# code

Let’s say you have a number scale that has a minimum and a maximum. You have a second number scale with a min and a max. How do you map an arbitrary value somewhere on that number scale to the second scale?

Let’s call the top scale a with a0 = 10 and a1 = 40. The bottom scale goes from b0 = -1 to b1 = 1. We want to map the value a = 20 to some value of b. The equation is:

$b=b_{0}+(b_{1}-b_{0})\displaystyle\frac{a-a_{0}}{a_{1}-a_{0}}$

If you are normalising a value to between 0 and 1, then b0 = 0 and b1 = 1. The equation reduces nicely to:

$b=\displaystyle\frac{a-a_{0}}{a_{1}-a_{0}}$

## C# code example

public double MapValue(double a0, double a1, double b0, double b1, double a)
{
return b0 + (b1 - b0) * ((a-a0)/(a1-a0));
}