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

Time:09-19

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.

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

## Better to replace your code to this.

m_perc = float(class_males)/float(class_total)* 100
f_perc = float(class_females)/float(class_total)* 100

male_perc = float("{:.2f}".format(m_perc))
female_perc = float("{:.2f}".format(f_perc))
  • Related