![]() |
VOOZH | about |
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 , 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)
Output
Decimal equivalent in % 33.33
In case the numerator is absolutely divisible by denominator, the number is returned to 1 place of decimal.
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)The special case where the denominator of the fraction is 1, that is, fractional numbers become equivalent to an integer value.
Output
Converting number to percentage rounding to 0 place of decimal
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 :
Output
Converting number to percentage rounding to 0 place of decimal 33% Converting number to percentage rounding to 2 place of decimal -50.00%