Calculate a number based on percentage

400 Views Asked by At

Current i have a certain percentage. (0% - 100%)

i need to convert this percentage to a number in a range. (-45 degrees - 45 degrees)

how would i do this?

so:

0% = -45 degrees 50% = 0 degrees 100% = 45 degrees

any help is appreciated

2

There are 2 best solutions below

1
On

The length of the range you have is 90. So first of all we have to rescale the percentage by $\frac{90}{100} = 0.9$. Then we translate by 45.

So if $x$ is your percentage, you want $0.9x - 45$

0
On

You may find this useful. The order-preserving linear map $\ell$ from the interval $[a,b]$ to the interval $[c,d]$ is given by $$\ell(x)= c + (d-c)\frac{x-a}{b-a}$$ In your particular case, take $[a,b]=[0,100]$ and $[c,d] = [-45,45]$. This gives $$\ell(x) = -45 +(45-(-45))\frac{x-0}{100-0}= \boxed{-45+0.9x}$$

Notice what the general formula does; when $x=a$, the fraction on the right is zero, so the image point is $c$. As $x$ changes from $a$ to $b$, the fraction changes from $0$ to $1$, so the contribution of the whole term on the right changes from $0$ to $d-c$. When $x$ reaches $b$, the value has grown to $c+(d-c)$, which is just $d$.

So as $x$ changes from $a$ to $b$, $\ell(x)$ changes from $c$ to $d$, as required.