Dev · Converter

Binary fraction converter: decimal to binary, and back.

Enter a decimal such as 0.1 or a fraction such as 1/3 and get the exact binary expansion, with a repeating block in parentheses when it never ends. Or enter bits such as 101.011 or 0.0(0011) and get the exact decimal. The arithmetic is exact (BigInt numerator and denominator), so nothing is rounded until the float64 step. Everything runs in your browser.

Fraction: repeated multiply-by-2 Reverse: sum of 2-k

Advertisement

Inputs

Decimal: 0.1, -12.375, 1e-3 or 1/3. Binary: 101.011, or a repeating tail in parentheses such as 0.0(0011).

Result

Enter a value and press Convert.

How a decimal fraction becomes binary

Convert the integer part by repeated division by 2. For the fractional part, multiply by 2, write down the integer part of the product (0 or 1) as the next bit, keep the fractional remainder, and repeat. For 0.375: 0.375 × 2 = 0.75 (bit 0), 0.75 × 2 = 1.5 (bit 1), 0.5 × 2 = 1.0 (bit 1), remainder 0, so 0.375 = 0.011 in binary. This is ordinary positional notation, described in Wikipedia’s binary number article.

Which fractions repeat, and how this page detects it

Write the fraction in lowest terms n/d. It terminates in binary exactly when d is a power of two. Otherwise the bits repeat forever. This page keeps the remainder as an exact BigInt fraction (never a float), skips the pre-period (the number of factors of 2 in d), then follows the remainders until one returns; the bits in between are the repeating block, shown in parentheses. The block length is the multiplicative order of 2 modulo the odd part of d (repeating expansions). Periods longer than 200,000 bits are reported as unresolved.

Why 0.1 repeats

0.1 = 1/10, and 10 = 2 × 5. The factor 5 is not a power of two, so 0.1 = 0.0(0011): a single 0, then 0011 repeating forever. A float64 has 53 significant bits (IEEE 754-2019), so it stores the nearest 53-bit value instead. The result panel shows that stored value and the exact difference from what you typed, computed live. JavaScript’s Number is a float64. To see the sign, exponent and mantissa fields of that stored value, use the IEEE 754 float converter.

Binary to decimal

A bit at position k after the point is worth 2-k, so 101.011 = 4 + 1 + 1/4 + 1/8 = 5.375. Every finite binary fraction has a finite decimal expansion, because 2 divides 10. A repeating tail such as 0.(01) is summed exactly as a geometric series: 0.(01) = 1/3.

Computed live in your browser for 0.1:

What this does and doesn’t

Results are exact, not rounded, except the float64 line, which uses round-to-nearest, ties-to-even, for values in the normal float64 range (about 2.2×10-308 to 1.8×10308). The float64 line is not shown outside that range. Long expansions are shown truncated with an ellipsis. Decimal exponents are limited to ±300.

Advertisement
Advertisement
Listening…