keepcalculator

06 Numbers and Powers

Binary Floating Point to Decimal Calculator in Python

Card No. 06·0361 · Updated · Standard formula, shown below

Exact stored value as a fraction 0.1
Decimal-looking input
0.1
You entered
You enter
Decimal-looking input
You get
Exact stored value as a fraction
Rounding
0 dp

A binary floating point to decimal calculator in Python can show the exact decimal value stored for a Python float, rather than its shorter, rounded display. Enter a decimal-looking value such as 0.1, and Python’s float methods reveal the binary approximation as an exact fraction or a full-precision decimal representation.

Formula

This calculator describes a Python float’s stored value as a rational number: x = numerator / denominator. In Python, x.as_integer_ratio() returns the numerator and denominator of that exact value. The input x is the value represented by a Python binary floating-point number; the fraction is exact, not a rounded decimal conversion.

Worked Example

For the default input, x = 0.1, Python interprets the literal as the nearest representable binary float. Its exact ratio is:

  • Numerator: 3602879701896397
  • Denominator: 36028797018963968
  • Stored value: 3602879701896397 / 36028797018963968

This fraction is the exact result. As a decimal, it is approximately 0.1000000000000000055511151231257827…; the ellipsis matters because its decimal expansion does not terminate. The usual display, 0.1, is a compact rounded rendering, not the full stored value.

Python code for the conversion

Python’s built-in float type normally uses IEEE 754 binary64 floating point. Use as_integer_ratio() when you need an exact, readable description of the value held by a float:

x = 0.1
numerator, denominator = x.as_integer_ratio()
print(numerator, denominator)
print(f"{numerator}/{denominator}")

To display the exact value as a decimal rather than a fraction, use the decimal module. Construct a Decimal from the float itself—not from its ordinary string display—to preserve the float’s exact value:

from decimal import Decimal

x = 0.1
print(Decimal.from_float(x))

This prints 0.1000000000000000055511151231257827021181583404541015625. By contrast, Decimal("0.1") represents exactly one tenth, because the string is parsed as a decimal number. These are different values, so choose the construction that matches what you are trying to inspect.

Why binary floats do not always equal decimal inputs

Computers store ordinary floats using powers of two. A finite binary fraction has the form of a sign, a significand, and a power-of-two scale. Some fractions, such as 0.5 (one half) and 0.25 (one quarter), have finite binary representations. Others do not. One tenth has a repeating binary expansion, much as one third has a repeating decimal expansion.

A float has a limited number of significant binary digits, so Python rounds a decimal input that cannot be represented exactly to the nearest available float. This is why evaluating 0.1 + 0.2 can produce 0.30000000000000004 instead of exactly 0.3. The result is a normal consequence of finite-precision arithmetic, not evidence that Python performed the addition incorrectly.

The calculator’s input is specifically a float value. If it reads the characters 0.1 and converts them to a float, it will show the approximation. If the goal is to preserve a decimal quantity exactly, use a decimal string with Decimal or represent the quantity as an integer ratio instead.

Choosing the right Python representation

  • Use float for general scientific and numerical work where fast approximate arithmetic is appropriate. Compare results using a tolerance when tiny rounding differences are expected.
  • Use Decimal when decimal digits and decimal rounding rules matter, such as calculations specified in decimal terms. Create values from strings, for example Decimal("0.1"), to avoid importing a float approximation.
  • Use Fraction when an exact rational value is useful. Fraction(1, 10) is exactly one tenth; Fraction.from_float(0.1) instead captures the exact fraction of the already-created float.

Changing the way a number is printed does not change its stored value. Formatting with a fixed number of decimal places rounds only the displayed text. Likewise, converting a float to a decimal string and back cannot recover decimal intent that was lost when the float was first created.

Common mistakes and limitations

  • Confusing display with storage: repr(0.1) is designed to show a short decimal that converts back to the same float. It is not the exact decimal expansion.
  • Passing a float to Decimal unintentionally: Decimal(0.1) preserves the float’s approximation. Use Decimal("0.1") when the intended input is decimal one tenth.
  • Expecting all arithmetic to be exact: even if an input is exactly representable, later operations can round. For example, repeated additions may accumulate small differences.
  • Treating the fraction as the original input: as_integer_ratio() reports the exact float currently stored. It cannot determine whether that float originally came from a decimal literal, a calculation, or another source.
  • Assuming every Python number is a float: Python also has arbitrary-precision integers and other numeric types. The ratio method applies to finite floats; special values such as infinity and NaN do not have an ordinary finite integer ratio.

There is no single universally best representation. A float-to-decimal calculator answers “what exact value does this float hold?” It does not answer “what decimal value did the person intend?” That distinction is essential when debugging numerical output or choosing a representation for an application.

Frequently Asked Questions

How do I convert a Python float to an exact decimal?
Use Decimal.from_float(value) to show the exact decimal expansion of a finite float. Use value.as_integer_ratio() for an exact fraction, which is often easier to inspect.

Why does Python show 0.1 instead of the long value?
Python displays a short decimal representation that uniquely identifies the stored float and converts back to that same value. The concise display does not mean the float is exactly one tenth.

Is Decimal(0.1) the same as Decimal("0.1")?
No. The string form creates exact decimal one tenth. The float form converts the binary approximation already stored by 0.1, so it contains many additional decimal digits.

How can I check whether two floats are close?
Use math.isclose(a, b, rel_tol=..., abs_tol=...) with tolerances chosen for the scale and purpose of the calculation. Exact equality is often unsuitable for results affected by rounding.

Can every decimal fraction be represented exactly as a float?
No. A reduced fraction has a finite binary representation only when its denominator is a power of two. Fractions such as one half are exact; one tenth is not.

Related

Formula used
in x | Decimal-looking input | - | 0.1 | -
out exact | Exact stored value as a fraction | - | x | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →