Here's one way (taken from a progress bar plugin):
This is JavaScript, but the language is of course irrelevant.
var max = 250; // the total amount representing 100%
var current = $obj.val().length; // the length we are measuring
// important: current / max * 100
// use min() to ensure that the result isn't greater than 100 due to rounding
var ratio = Math.min( ( ( current / max ) * 100 ), 100 );
The key piece is dividing the current amount by the total amount, where the total amount (whatever it may be) represents 100%. That division operation should yield a value less than 1. To convert that to a 1-100 percentage, multiply by 100.
In my above example, if current
= 125, and max
= 250, then we have 125 / 250 = 0.5. Multiplying 0.5 * 100 gives 50. 125 is 50% of 250.
If you wanted to get both percentages, you could simply subtract the first result from 100.
Here is a complete working example of a HTML/CSS/jQuery progress bar for managing the maxlength of a textbox: http://jsfiddle.net/QfZPt/1/