Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert % to a number
#1
I have a number in a range form 1 to 254
lets say I I have an input of 20%
I need to convert that % into a number with in that range
I know the number is about 50 but what would be the code to convert that percent into a number
Reply
#2
A percent is just a fraction. 20% => 20/100.

Your top is 254 and the bottom is (near) zero, so multiply. If you got an input of 0%, would you want the answer to be 0 or 1?

>>> amount = (20/100) * 254
>>> amount
50.800000000000004
Reply
#3
The formula for converting from % to number is:
number = percent / 100 * (max - min) + min.

To Test:
100% = 100/100 * (254 - 1) + 1 = 254
0% = 0 / 100 * (254 - 1) + 1 = 1
20% = 20 / 500 ( (254 - 1) + 1 = 51.6

And the formula for converting a number to a percent is:
percent = 100 *(number - min) / (max - min)

To test:
254 = 100 * (254 - 1) / (254 - 1) = 100%
1 = 100 * (1 - 1) / (254 - 1) = 0%
51.6 = 100 * (51.6 - 1) / (254 - 1) = 20%
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert value to defined number? philipbergwerf 7 3,322 Dec-08-2019, 01:17 PM
Last Post: philipbergwerf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020