com.fasterxml.jackson.core.io.schubfach

## Class DoubleToDecimal

- java.lang.Object
- - com.fasterxml.jackson.core.io.schubfach.DoubleToDecimal

    public final class DoubleToDecimal
    extends Object

This class exposes a method to render a `double` as a string.

Author:
  Raffaello Giulietti

### Field Summary

Fields
`int` `MAX_CHARS`

### Method Summary

All Methods Static Methods Concrete Methods
`static Appendable` `appendTo(double v, Appendable app)` Appends the rendering of the `v` to `app`.
`static String` `toString(double v)` Returns a string rendering of the `double` argument.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### MAX_CHARS

    public final int MAX_CHARS

See Also:
  Constant Field Values

### Method Detail

#### toString

    public static String toString(double v)

Returns a string rendering of the `double` argument.

The characters of the result are all drawn from the ASCII set.

- Any NaN, whether quiet or signaling, is rendered as `"NaN"`, regardless of the sign bit.
- The infinities +∞ and -∞ are rendered as `"Infinity"` and `"-Infinity"`, respectively.
- The positive and negative zeroes are rendered as `"0.0"` and `"-0.0"`, respectively.
- A finite negative `v` is rendered as the sign '`-`' followed by the rendering of the magnitude -`v`.
- A finite positive `v` is rendered in two stages:
  - Selection of a decimal: A well-defined decimal d`v` is selected to represent `v`.
  - Formatting as a string: The decimal d`v` is formatted as a string, either in plain or in computerized scientific notation, depending on its value.

A decimal is a number of the form d×10i for some (unique) integers d > 0 and i such that d is not a multiple of 10. These integers are the significand and the exponent, respectively, of the decimal. The length of the decimal is the (unique) integer n meeting 10n-1 ≤ d < 10n.

The decimal d`v` for a finite positive `v` is defined as follows:

- Let R be the set of all decimals that round to `v` according to the usual round-to-closest rule of IEEE 754 floating-point arithmetic.
- Let m be the minimal length over all decimals in R.
- When m ≥ 2, let T be the set of all decimals in R with length m. Otherwise, let T be the set of all decimals in R with length 1 or 2.
- Define d`v` as the decimal in T that is closest to `v`. Or if there are two such decimals in T, select the one with the even significand (there is exactly one).

The (uniquely) selected decimal d`v` is then formatted.

Let d, i and n be the significand, exponent and length of d`v`, respectively. Further, let e = n + i - 1 and let d1…dn be the usual decimal expansion of the significand. Note that d1 ≠ 0 ≠ dn.

- Case -3 ≤ e < 0: d`v` is formatted as `0.0`…`0` d1…dn, where there are exactly -(n + i) zeroes between the decimal point and d1. For example, 123 × 10-4 is formatted as `0.0123`.
- Case 0 ≤ e < 7:
  - Subcase i ≥ 0: d`v` is formatted as d1…dn`0`…`0.0`, where there are exactly i zeroes between dn and the decimal point. For example, 123 × 102 is formatted as `12300.0`.
  - Subcase i < 0: d`v` is formatted as d1…dn+i.dn+i+1…dn. There are exactly -i digits to the right of the decimal point. For example, 123 × 10-1 is formatted as `12.3`.
- Case e < -3 or e ≥ 7: computerized scientific notation is used to format d`v`. Here e is formatted as by `Integer.toString(int)`.
  - Subcase n = 1: d`v` is formatted as d1`.0E` e. For example, 1 × 1023 is formatted as `1.0E23`.
  - Subcase n > 1: d`v` is formatted as d1`.`d2…dn`E` e. For example, 123 × 10-21 is formatted as `1.23E-19`.

Parameters:
  `v` - the `double` to be rendered.
Returns:
  a string rendering of the argument.

#### appendTo

    public static Appendable appendTo(double v,
                                      Appendable app)
                               throws IOException

Appends the rendering of the `v` to `app`.

The outcome is the same as if `v` were first `rendered` and the resulting string were then `appended` to `app`.

Parameters:
  `v` - the `double` whose rendering is appended.
  `app` - the `Appendable` to append to.
Throws:
  `IOException` - If an I/O error occurs