Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Scale Numbers from 0.0f to 1.0f

1. ## 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:
```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.
```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:
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

2. ## Re: Scale Numbers from 0.0f to 1.0f

Nevermind, I was dividing by zero in my origional program :X