0

I am trying to get some percentages to print to 4 decimal places. My code looks like this

for i in range(len(candidates)):  #looping through the results
    percentage_votes = "{:.4%}".format(results[i][1] / voters)           #setting the number to a %
    print(f'{results[i][0]}: {percentage_votes} ({results[i][1]})')      #print result

When it prints; however, rather than giving me the decimal places of the division, it instead returns all zeros in the 4 decimal places.

Khan: 63.0000% (2218231)
Correy: 20.0000% (704200)
Li: 14.0000% (492940)
O'Tooley: 3.0000% (105630)

What do I need to correct in my code to give me the further divided number? Thanks!

  • 1
    Does this answer your question? [Fixed digits after decimal with f-strings](https://stackoverflow.com/questions/45310254/fixed-digits-after-decimal-with-f-strings) – Chris Sep 21 '20 at 17:00

1 Answers1

0

I tried this

"{:.4%}".format(3.4445453543) 

Output: '344.4545%'

Which got me thinking that might be the division is not reporting float,

can you try this:

"{:.4%}".format(results[i][1] / float(voters)) 
Ajay Verma
  • 610
  • 2
  • 12