Maths Range To Percentage

后端 未结 3 895
离开以前
离开以前 2021-02-02 02:36

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.

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 03:20

    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.

提交回复
热议问题