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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Scale Numbers from 0.0f to 1.0f

  1. #1
    Join Date
    Apr 2011
    Thanked 1 Time in 1 Post

    Default 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:
    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

  2. #2
    Join Date
    Apr 2011
    Thanked 1 Time in 1 Post

    Default Re: Scale Numbers from 0.0f to 1.0f

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

Similar Threads

  1. Can't get seven random numbers, only one.
    By alpvii in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2010, 05:39 PM
  2. disticnt numbers
    By Fordy252 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 21st, 2010, 11:40 AM
  3. Help with sorting numbers
    By planktonx in forum Collections and Generics
    Replies: 2
    Last Post: October 5th, 2010, 08:01 AM
  4. adding up odd and even numbers
    By darlinho in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2010, 03:28 PM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM