# Scale Numbers from 0.0f to 1.0f

• September 23rd, 2011, 08:51 PM
techwiz24
Scale Numbers from 0.0f to 1.0f
Here's the problem.

I am trying to implement a custom slider in a game written in java. Due to the nature of the game engine, the slider only supports values from 0.0f to 1.0f. In reality, I may need to have values from 0-20 or 1-4.

Converting from the slider value to the scale value is simple:
Code :

```protected float sliderToActual(){ return (sliderValue*maxValue); }```

The other way around is what confuses me. In the constructor, I set the value (not in the 0-1 scale), the minimum value (ex: 1) and the max value (ex: 20).

Here's some code I wrote to test out the algorithm.
Code :

```public static void main(String[] args) { float minValue = 2; float maxValue = 20; for (int i=1; i<21; i++) System.out.println(i + " on the scale is " + (i - minValue)/( maxValue - minValue)); }```

Which gives the output of:
Quote:

Originally Posted by Output
1 on the scale is -0.055555556
2 on the scale is 0.0
3 on the scale is 0.055555556
4 on the scale is 0.11111111
5 on the scale is 0.16666667
6 on the scale is 0.22222222
7 on the scale is 0.2777778
8 on the scale is 0.33333334
9 on the scale is 0.3888889
10 on the scale is 0.44444445
11 on the scale is 0.5
12 on the scale is 0.5555556
13 on the scale is 0.6111111
14 on the scale is 0.6666667
15 on the scale is 0.7222222
16 on the scale is 0.7777778
17 on the scale is 0.8333333
18 on the scale is 0.8888889
19 on the scale is 0.9444444
20 on the scale is 1.0

• September 23rd, 2011, 09:50 PM
techwiz24
Re: Scale Numbers from 0.0f to 1.0f
Nevermind, I was dividing by zero in my origional program :X