How to convert fraction to percent in Python?
Last Updated :
24 Jan, 2021
Fractions are a part of a whole commodity that can be expressed in the form of a numerator divided by the denominator. A fraction is of the form \pm\frac{p}{q} , where p<=q.
There are various methods of converting between fractions and percentages.
Method 1:
Manual conversion of the fraction to percentage can be done by multiplying the rational number by 100. The obtained decimal number may be rounded up to the required number of digits by using the inbuilt math.round() function which has the following syntax:
round(number, digits)
Python3
# 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))
Output
Decimal equivalent in %
33.33
In case the numerator is absolutely divisible by denominator, the number is returned to 1 place of decimal.
Python3
# 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))
Output
Decimal equivalent in %
50.0
Method 2:
The str.format() method is used to convert the number into a percentage, by specifying the number of digits to take after the decimal point.
"{:.n%}".format(num)
- n represents the number of digits after the decimal point
- num represents the floating-point number expressed either as a decimal or a fraction
The special case where the denominator of the fraction is 1, that is, fractional numbers become equivalent to an integer value.
Python3
# 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))
Output
Converting number to percentage rounding to 0 place of decimal
Python3
# 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%
While converting integers, the number of decimal places does not play any role, since the number is directly multiplied by 100. The number of zeros appended to the number is equivalent to the number of decimal places chosen.
The following Python code is used to convert a rational number to an equivalent percentage :
Python3
# 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))
Output
Converting number to percentage rounding to 0 place of decimal
33%
Converting number to percentage rounding to 2 place of decimal
-50.00%
Similar Reads
Convert String to Float in Python The goal of converting a string to a float in Python is to ensure that numeric text, such as "33.28", can be used in mathematical operations. For example, if a variable contains the value "33.28", we may want to convert it to a float to perform operations like addition or division. Let's explore dif
2 min read
Cannot Convert String To Float in Python Python, a versatile and powerful programming language, is widely used for data manipulation and analysis. However, developers often encounter challenges, one of which is the "Cannot Convert String To Float" error. This error occurs when attempting to convert a string to a float, but the string's con
3 min read
How to convert Decimal To Percent with Solved Examples Decimal to percent conversion is very useful in number system. Decimal is derived from the Latin word Decimus which implies one-10th. The decimal framework has a base of 10. It is normally perceived by the dot ", " between the digits called "decimal point". The expression "percent" is comprised of t
5 min read
How to change a percent to a whole number? To change a percentage to a whole number, you can simply remove the percentage sign (%) and divide the number by 100. This effectively converts the percentage to a decimal, which can then be represented as a whole number. Whole numbers are a set of numbers that include all the natural numbers (count
4 min read
Different Ways to Escape percent (%) in Python strings In Python, the percent sign (%) plays a crucial role, especially in string formatting and as a modulo operator. However, there are times when we want to use the percent sign as a literal character in a string rather than as a placeholder for formatting. In such cases, knowing how to escape the perce
4 min read