0

I want to calculate the percentage between two numbers.

Before asking here, I looked at other pages and questions such as:

None of the pages above helped me.

The problem:

I have, two numbers: 3 upvotes and 2 downvotes, and I want to calculate the percentage of how many people upvoted the message in relation to the downvotes.

My desired outcome is that the higher the upvotes get, the higher the calculated percentage gets.

Example:

3 upvotes and 3 downvotes: 50% upvotes 4 upvotes and 3 downvotes: XX% upvotes (Higher than 50%)

Here is what I have tried to do:

percentage = (data[str(payload.message_id)]['downvote'] / data[str(payload.message_id)]['upvote']) * 100
# The higher it gets, the lower the number gets -> 10 to 6 = 60%

((data[str(payload.message_id)]['downvote'] / data[str(payload.message_id)]['upvote']) * 100) / 2
# Also calculates things wrong -> 16 to 6 = 37.5%

float(data[str(payload.message_id)]['upvote'])-data[str(payload.message_id)]['downvote'])/data[str(payload.message_id)]['downvote'])*100

Maybe this is just a simple mistake I made, but I am not seeing it. data[str(payload.message_id)]['upvote'] and data[str(payload.message_id)]['downvote'] are obviously the numbers I saved somewhere.

Dominik
  • 3,612
  • 2
  • 11
  • 44

2 Answers2

1
fraction = data[str(payload.message_id)]['upvote'] / (data[str(payload.message_id)]['upvote'] + data[str(payload.message_id)]['downvote'])
print(f"{fraction:.2%}")
mave_rick
  • 59
  • 8
0

If you have 2 numbers a,b their percentages must be in relation to their sum a + b. If you have 3 upvotes and 3 downvotes,

percentage_upvote = (3 / 6) * 100 = 50%

For your second case (16, 6),

percentage_upvote = (16 / 22) * 100 = 72.73
Brian Koech
  • 183
  • 1
  • 5
  • Hey, Brian, this helped me. Even though the other answer is also helping me, I am accepting yours as I do not have to do any "extra" step. – Dominik May 01 '22 at 08:34