Displaying 100% from a number?

Okay, so I’ve got two variables that I need to know what percent they are. I got a bar that goes up to 100, and the first variable is 120, and the second one is (100 - 60) it’s the 60 in that equation. Anyway, how do I show them from only 1 to 100?


//draw particle bar background
    	g.setColor(new Color(0,0,255));
    	g.fillRect(10, 170, 100, 5);
    	
    	if(player.particleMode){
    		g.setColor(new Color(0,255,255));
        	g.fillRect(10, 170, player.particleTime, 5);
    	}
    	
    	else{
        		g.setColor(new Color(255,0,0));
            	g.fillRect(10, 170, 100 - player.particleDelay, 5);
    	}

This code should do it for you:


/**
 * The same as the other percent, only it presumes the range is from 0 to
 * max.
 * 
 * @param val The value to calculate the percentage of from the range.
 * @param max The maximum value in the range.
 */
public float percent(float val, float max)
{
    return percent(val, 0, max);
}

/**
 * This takes a range (defined by min and max) and return what percentage
 * the given val takes in that range. The percent is returned as a decimal
 * value where 0 is 0% and 1 is 100%.
 * 
 * For example the percentage of 150 in teh range 100 to 200 will return
 * 0.5 (50%).
 * 
 * @param val The value to get the percentage of.
 * @param min The minimum value in the range.
 * @param max The maximum value of the range.
 */
public float percent(float val, float min, float max)
{
    return (val-min) / (max-min);
}

I don’t quite understand the range of values your converting, but you want the percentage of player.particleTime between 60 and 120? Then it’s:


    float percent = percent( player.particleTime, 60, 120 );
    int width = (int) (100 * percent);

When the counter is at 120 I need a variable to say 100. When it’s at 60 I need a variable to say 50.

    float percent = percent( counter, 120 );
    int percentOf100 = (int) (100 * percent);

Ah, alright. thanks. ;D

No problem.

I know it’s tiny but if you (or anyone else) uses the code directly then please add an url to this topic in the method headers, just so it’s credited.

[quote]public float percent(float val, float min, float max)
{
return (val-min) / (max-min);
}
[/quote]
you need to check if min==max.

Oh, please add an extra url to this post in the method headers if you include this check, just so it’s extra credited.
(just kidding :))

Just so you don’t have to provide a URL, you can use mine:


public void draw()
{
     float clipX = (value - minimum) / (maximum - minimum);
     clipX = Math.min(clipX, 1.0f);
     clipX = Math.max(clipX, 0.0f);
     g.fillRect(x, y, width * clipX, height);
}

Yes I know it’s the same thing, but it’s not like this “algorithm” is secret or anything. It’s just simple arithmetic. I can quote at least 10 places in my own games where I’ve used it, except obvious I didn’t bother to make a getPercent() method in any of them, I just calculate what I need when I need it, as it’s a simple operation.

To be honest, I was just saying that to be nice >_>
I ended up using a simple (float) (var * (var/100)) in my drawing.