Fractions, Decimals and Percents
Fractions, decimals and percents are three ways to write the same number: . This lesson collects all six conversions in one place, gives a table of values worth knowing by heart, and shows Python's conversion tools, including how to recover a simple fraction from a messy float.
The six conversions
÷ (top by bottom) × 100, add %
fraction ────────────────────► decimal ───────────────► percent
◄──────────────────── ◄───────────────
write over 10, 100, … ÷ 100, drop %
and simplify| From → To | How | Example |
|---|---|---|
| fraction → decimal | divide numerator by denominator | |
| decimal → percent | multiply by 100 (point 2 places right) | |
| percent → decimal | divide by 100 (point 2 places left) | |
| decimal → fraction | write over a power of 10, simplify | |
| fraction → percent | fraction → decimal → percent | |
| percent → fraction | write over 100, simplify |
Tip: For fraction → percent with a "nice" denominator, scale to 100 directly: , .
Values worth memorising
| Fraction | Decimal | Percent |
|---|---|---|
| 0.5 | 50% | |
| 0.25 | 25% | |
| 0.75 | 75% | |
| 0.2 | 20% | |
| 0.125 | 12.5% | |
| 0.375 | 37.5% | |
| 0.1 | 10% | |
| 0.05 | 5% | |
| 0.01 | 1% |
From these you can build others: , and .
Repeating decimals and percents
Fractions whose denominator has prime factors other than 2 and 5 give repeating decimals. You can write the percent exactly as a mixed number () or round it (33.3%). Rounding loses information, so convert at the very end, for display.
Conversions in Python
from fractions import Fraction
from decimal import Decimal
f = Fraction(3, 8)
print("fraction -> decimal:", float(f), Decimal(f.numerator) / Decimal(f.denominator))
print("fraction -> percent:", f"{float(f):.1%}")
print("percent -> fraction:", Fraction(85, 100), " ", Fraction("6.5") / 100)
print("decimal -> fraction:", Fraction("0.065"), " ", Fraction(Decimal("0.375")))
for text in ["2/5", "7/20", "1/3", "5/6"]:
x = Fraction(text)
print(f"{text:>5} = {float(x):.4f} = {float(x):.2%}")fraction -> decimal: 0.375 0.375
fraction -> percent: 37.5%
percent -> fraction: 17/20 13/200
decimal -> fraction: 13/200 3/8
2/5 = 0.4000 = 40.00%
7/20 = 0.3500 = 35.00%
1/3 = 0.3333 = 33.33%
5/6 = 0.8333 = 83.33%
From a float back to a fraction
A float is stored in binary, so converting it to a fraction directly gives its exact binary value, which is usually ugly. limit_denominator(n) finds the closest fraction with a denominator of at most , which recovers the "intended" fraction:
from fractions import Fraction
x = 0.1
print(Fraction(x)) # exact binary value of the float
print(Fraction(x).limit_denominator(1000))
ratio = 1920 / 1080 # an aspect ratio as a float
print(ratio, "->", Fraction(ratio).limit_denominator(100))
print(Fraction(3.14159265).limit_denominator(10)) # a famous approximation of π
print(Fraction(3.14159265).limit_denominator(1000))3602879701896397/36028797018963968
1/10
1.7777777777777777 -> 16/9
22/7
355/113
and are the best fractions for with small denominators; is right to six decimal places.
In software
- Storage vs display: store rates as decimals or exact fractions (
0.075,Fraction(3, 40)), show them as percents (7.5%). Never store the display string. - APIs and units: one API sends
"discount": 15(percent), another"discount": 0.15(fraction). Mixing them up gives a discount 100 times too big or too small. Name fields clearly (discount_pct,discount_rate). - Parsing user input: a form that accepts "1/2", "0.5" or "50%" can normalise all three to a
Fraction.
Exercises
Exercise 1: Fill the table
Convert to a decimal and percent, 0.125 to a fraction and percent, and 140% to a decimal and fraction.
Answer
; ; .
Solution
Exercise 2: Percent with a repeating part
Write 5/12 as a percent, exactly (as a mixed number) and rounded to one decimal place.
Answer
Solution
. As a decimal , which rounds to 41.7%.
Exercise 3: Order mixed forms
Order from smallest to largest: 0.62, , 61%, .
Answer
Solution
Convert all to decimals: 0.62, 0.625, 0.61, 0.6. Sorted: .
Exercise 4: A forgiving parser
Write parse(text) that accepts "1/2", "0.5", "50%" or "12.5%" and returns a Fraction. Test it on "3/8", "0.375", "37.5%" and "150%".
Answer
, , and .
Solution
A trailing % means "divide by 100". Fraction already understands "3/8" and "0.375".
from fractions import Fraction
def parse(text):
text = text.strip()
if text.endswith("%"):
return Fraction(text[:-1]) / 100
return Fraction(text)
for t in ["3/8", "0.375", "37.5%", "150%"]:
print(t, "->", parse(t))3/8 -> 3/8
0.375 -> 3/8
37.5% -> 3/8
150% -> 3/2
Test yourself
What is as a percent?
- 78%
- 8.75%
- 87.5%
- 0.875%
What is 0.4% as a decimal?
- 0.4
- 0.04
- 0.004
- 4
What does Fraction(0.25) give?
- 1/4
- a huge fraction
- 25/100
- an error