Fractions that are expressed in the form of a numerator can be the most important asset can be divided by a denominator in Python. Let’s explore the numerous methods for converting fractions into percentages in python.
A fraction is of the form +-p/q
, where p<=q.
Also Read: How to Run a Python Script
Manual Conversion
The Manual method of converting fractions into percentages in python is done by multiplying a rational number by 100. By using the inbuilt math.round()
function, the acquired decimal number can be rolled out by the required digits.
round(number, digits)
#define a number
num = 1/3
#converting to decimal value
dec_num = num * 100
#rounding number to required number of decimal places
print ("Decimal equivalent in %")
#round to 2 places
print (round(dec_num,2))
Decimal equivalent in % 33.33
If there’s a possibility of the numerator being divisible by the denominator, the number is returned to 1st place of decimal.
#define a number
num = 2/4
#converting to decimal value
dec_num = num * 100
#rounding number to required number of decimal places
print ("Decimal equivalent in %")
#round to 2 places
print (round(dec_num,4))
Decimal equivalent in % 50.0
str. format() method
The str.format()
the method is known for converting fractions into percentages by specifying the number of digits taken after the decimal point.
"{:.n%}".format(num)
- n = the number of digits after decimal point
- num = the floating-point number expressed either as a decimal or a fraction otherwise.
In rare cases, if the denominator of the fraction is 1, then the fractional numbers will become equivalent to their integer value.
#input an integer with denominator =1
integer_num = 2
print ("Converting number to percentage rounding to 0 place of decimal")
print ("{:.0%}".format(integer_num))
#input an negative integer with denominator =1
integer_num = -7
print ("Converting number to percentage rounding to 5 place of decimal")
print ("{:.5%}".format(integer_num))
Converting number to percentage rounding to 0 place of decimal
#input an integer with denominator =1
integer_num = 2
print ("Converting number to percentage rounding to 0 place of decimal")
print ("{:.0%}".format(integer_num))
#input an negative integer with denominator =1
integer_num = -7
print ("Converting number to percentage rounding to 5 place of decimal")
print ("{:.5%}".format(integer_num))
200% Converting number to percentage rounding to 0 place of decimal -700.00000%
The numbers of the decimal places are not responsible while converting the integers since the numbers are directly multiplied by 100. The number of zeros conjoined to the value is similar to the number of chosen decimal places.
The below example shows how to convert a rational number to a similar percentage.
#input a fractional number
num = 1/3
#taking 0 places to decimal and convert it to decimal
print ("Converting number to percentage rounding to 0 place of decimal")
print ("{:.0%}".format(num))
num = -2/4
#taking 2 places to decimal and convert it to decimal
print ("Converting number to percentage rounding to 2 place of decimal")
print ("{:.2%}".format(num))
Converting number to percentage rounding to 0 place of decimal 33%
Converting number to percentage rounding to 2 place of decimal -50.00%
Bookmark this site to understand more python concepts.