How do I get my output for percentage to be two digits?

Question:

I am trying to get a clean percentage from this line of code. For example, 50.00% or 61.37%. From this line, I am getting 5000.00% and so on. I have an idea that it has to do with how I am using format, but I’m not sure how it is doing this. I tried using int and got the desired number, just without the .00 and the % sign.

class_males = float(input('Please enter the number of male students registered:')) #Asks for user input of males
class_females = float(input('Please enter the number of female students registered:'))#Asks for user input of females
class_total = int(class_males) + int(class_females) #class total by combining user inputs
m_perc = int(class_males)/int(class_total)* 100 #divide user input of males by class total then multiply by 100 for %
f_perc = int(class_females)/int(class_total)* 100 #divide user input of females by class total then multiply by 100 for %
male_perc = "{:.2%}".format(m_perc) #adds "%" sign after number total 
female_perc = "{:.2%}".format(f_perc) #adds "%" sign after number total 
print('Percentage of males registered:', (male_perc)) #display percent of males 
print('Percentage of females registered:',(female_perc))  #display percent of females

If it isn’t already clear, I am a beginner. Thank you for any help provided.

Asked By: Wisroix

||

Answers:

You are multiplying by 100 twice because the % format specifier already does that for you, but you also do it yourself, resulting in a multiplication by 10,000 at the end.

See docs:

Type Meaning
'%' Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign.

(Emphasis mine.)

The solution resulting in the leanest code is therefore to remove your own multiplication by 100 and just pass the ratio value directly:

m_ratio = int(class_males) / int(class_total) # divide user input of males by class total
f_ratio = int(class_females) / int(class_total) # divide user input of females by class total
male_perc = "{:.2%}".format(m_ratio) # multiplies by 100 and adds "%" sign after number total 
female_perc = "{:.2%}".format(f_ratio) # multiplies by 100 and adds "%" sign after number total 

Alternatively, you could switch to formatting as float using f as explained in Claudio’s answer.

Answered By: CherryDT

Small typo: you have placed the % character in the wrong place in the format specification. Put it as follows:

"{:6.2f}%" ( instead of "{:.2%}" )

You are getting the wrong result because the ‘%’ sign used as format directive within {} formats a number to %-value by multiplication with 100, adding the % sign at its end.

Alternatively, you could switch to changing your m_perc,f_perc values to m_ratio and f_ratio as explained in CherryDTs answer.

Answered By: Claudio

The problem is not with how you are using format (from my testing, although I am also fairly new), but if you stop multiplying by 100 after, it should give proper results.

class_males = float(input('Please enter the number of male students registered:')) #Asks for user input of males
class_females = float(input('Please enter the number of female students registered:'))#Asks for user input of females
class_total = int(class_males) + int(class_females) #class total by combining user inputs
m_perc = int(class_males)/int(class_total) #divide user input of males by class total for %
f_perc = int(class_females)/int(class_total)#divide user input of females by class total for %
male_perc = "{:.2%}".format(m_perc) #adds "%" sign after number total 
female_perc = "{:.2%}".format(f_perc) #adds "%" sign after number total 
print('Percentage of males registered:', (male_perc)) #display percent of males 
print('Percentage of females registered:',(female_perc))  #display percent of females
Answered By: ZRomave
## Better to replace your code to this.

male_perc = "{:.2f}%".format(m_perc)
female_perc = "{:.2f}%".format(f_perc)
Answered By: Ahmed Arshed
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.