20

i need a bit of help with some maths.

I have a range 0 - 127 and i want to convert it into percentage.

So 0% = 0 and 100% = 127 and every number inbetween.

How would i do this?

Edit:

Thanks to what jon posted, i came up with:

$percent * 127 / 100

easwee
  • 15,757
  • 24
  • 60
  • 83
Ozzy
  • 10,285
  • 26
  • 94
  • 138

3 Answers3

46

While Jon's answer was not incorrect, the answer given by belisarius was more complete, in that it allowed for a range of numbers beginning and ending with any number, and not necessarily starting with 0.

Here's a little better way to represent the formula:

percentage = (value - min) / (max - min)

If you want to represent the percentage as a whole number instead of a decimal, simply multiply the result by 100.

And here's the reverse (going from a percentage to a value):

value = ((max - min) * percentage) + min

The percentage here is a decimal. If your percentage is a whole number, simply divide it by 100 before inserting in this formula.

blkhwks19
  • 503
  • 5
  • 13
  • Does a formula like this exist for any range, not only from min to max like 0 -> 20 but also something like 20 -> 0, so that when the percentage increases the value decreases until 100% is 0? – alessandro gaboardi Dec 10 '18 at 10:46
21

Generally, if you have numbers in the interval [a,b], to get the percentage inside your interval, the formula is:

percentage = 100 * (x-a) / (b-a)  

Where x is your value

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
6

If you want to go from a value to a percentage, eg from 63.5 to 50%, divide your value by 127 & multiply by 100.

If you want to go the other way, eg from 50% to 63.5, it's the reverse: divide your percentage by 100 & multiply by 127.

Jon
  • 16,212
  • 8
  • 50
  • 62