Algorithm to convert an IEEE 754 double to a string?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting an IEEE 754 double to a decimal string looks simple until you require the result to be correct, round-trippable, and short. The bit layout is easy to decode, but producing the shortest decimal string that parses back to the same binary floating-point value is a specialized algorithmic problem, which is why mature runtimes use algorithms such as Dragon4, Grisu, or Ryu rather than a naive repeated-division implementation.
Start With the IEEE 754 Layout
A double-precision IEEE 754 value has:
- 1 sign bit
- 11 exponent bits
- 52 fraction bits
In normalized form, the value is interpreted as:
- sign
- times significand
- times a power of two
A simple bit extraction example in C looks like this:
This tells you what the bits are, but it does not yet solve the decimal-formatting problem.
Handle Special Values First
A complete algorithm starts by handling the special cases separately:
- exponent all ones and fraction zero means infinity
- exponent all ones and fraction nonzero means NaN
- exponent zero and fraction zero means signed zero
- exponent zero and fraction nonzero means subnormal number
These branches are straightforward compared with ordinary finite formatting.
Special-case handling matters because these values do not follow the normal implicit-leading-one rule.
Why Naive Decimal Conversion Fails
A tempting approach is:
- reconstruct the binary floating-point value as a rational number
- repeatedly multiply or divide to get decimal digits
- stop when the output looks accurate enough
The problem is that "accurate enough" is not a rigorous stopping rule. Correct conversion needs to respect rounding boundaries so that:
- the printed string parses back to the exact same double
- the string is not longer than necessary
- halfway cases round correctly
This is where simple homemade algorithms usually go wrong.
The Real Problem: Shortest Correct Round-Trip String
Modern float-to-string algorithms solve a more exact task:
- compute the decimal interval that rounds back to the original double
- generate the shortest decimal inside that interval
- format it efficiently
Ryu is famous because it computes this quickly using integer arithmetic and precomputed powers. Grisu is very fast but may need fallback logic. Dragon4 is older, flexible, and correct, but often more complex.
The key point is that serious float formatting algorithms do not merely "print many digits." They reason about the decimal interval that preserves the original binary value.
A Practical Strategy: Use the Runtime Implementation
Unless you are writing a language runtime, serializer, or numeric library, the right answer is usually to use the standard library formatter.
For example, in Java:
In Python:
Those runtime implementations already use high-quality float-formatting logic. Reimplementing them from scratch is rarely worth it unless the algorithm itself is your project.
If You Must Implement It
If your actual task is to implement the conversion algorithm, the high-level structure is:
- decode sign, exponent, and fraction
- classify special cases
- normalize the significand and binary exponent
- compute decimal boundaries for the exact rounding interval
- generate digits inside that interval
- choose fixed or scientific notation formatting
The difficult part is step four and step five. That is where Ryu, Grisu, and Dragon4 differ.
So the honest engineering advice is:
- use a published algorithm
- or reuse a known-good implementation
- do not invent the rounding logic ad hoc
Common Pitfalls
- Extracting the IEEE 754 fields correctly but then using a naive decimal-generation loop still produces incorrect or overlong strings.
- Ignoring NaN, infinity, signed zero, and subnormal values leaves the implementation incomplete.
- Printing too many digits may be correct but ugly; printing too few can break round-tripping.
- Assuming decimal formatting is just base conversion misses the need for exact rounding boundaries.
- Reimplementing this from scratch without a known algorithm such as Ryu, Grisu, or Dragon4 is a common path to subtle numeric bugs.
Summary
- Decoding the bits of a double is easy compared with formatting it correctly as decimal.
- The hard part is generating the shortest decimal string that round-trips back to the same IEEE 754 value.
- Special values such as NaN, infinity, signed zero, and subnormals must be handled explicitly.
- Production-quality runtimes use dedicated algorithms such as Ryu, Grisu, or Dragon4.
- If you are not writing a runtime or numeric library, use the language's built-in
doubleformatting instead of implementing the algorithm yourself.

