Top Description Inners Fields Constructors Methods
org.apache.commons.text.numbers

public final Enum DoubleFormat

extends Enum<DoubleFormat>
Class Inheritance
Imports
java.text.DecimalFormatSymbols, java.util.Objects, java.util.function.DoubleFunction, .Function, .Supplier

Enum containing standard double format types with methods to produce configured formatter instances. This type is intended to provide a quick and convenient way to create lightweight, thread-safe double format functions for common format types using a builder pattern. Output can be localized by passing a DecimalFormatSymbols instance to the formatSymbols method or by directly calling the various other builder configuration methods, such as digits.

Comparison with DecimalFormat

This type provides some of the same functionality as Java's own java.text.DecimalFormat. However, unlike DecimalFormat, the format functions produced by this type are lightweight and thread-safe, making them much easier to work with in multi-threaded environments. They also provide performance comparable to, and in many cases faster than, DecimalFormat.

It should be noted that the output String is created by formatting the output of Double#toString(). This limits the output precision to the precision required to exactly represent the input double and is dependent on the JDK implementation of Double#toString(). A number formatted with the maximum precision should be parsed to the same input double. This implementation cannot extend the String to the required length to represent the exact decimal value of the double as per BigDecimal#toString().

Examples

// construct a formatter equivalent to Double.toString()
DoubleFunction<String> fmt = DoubleFormat.MIXED.builder().build();

// construct a formatter equivalent to Double.toString() but using
// format symbols for a specific locale
DoubleFunction<String> fmt = DoubleFormat.MIXED.builder()
     .formatSymbols(DecimalFormatSymbols.getInstance(locale))
     .build();

// construct a formatter equivalent to the DecimalFormat pattern "0.0##"
DoubleFunction<String> fmt = DoubleFormat.PLAIN.builder()
     .minDecimalExponent(-3)
     .build();

// construct a formatter equivalent to the DecimalFormat pattern "#,##0.0##",
// where whole number groups of thousands are separated
DoubleFunction<String> fmt = DoubleFormat.PLAIN.builder()
     .minDecimalExponent(-3)
     .groupThousands(true)
     .build();

// construct a formatter equivalent to the DecimalFormat pattern "0.0##E0"
DoubleFunction<String> fmt = DoubleFormat.SCIENTIFIC.builder()
     .maxPrecision(4)
     .alwaysIncludeExponent(true)
     .build()

// construct a formatter equivalent to the DecimalFormat pattern "##0.0##E0",
// i.e. "engineering format"
DoubleFunction<String> fmt = DoubleFormat.ENGINEERING.builder()
     .maxPrecision(6)
     .alwaysIncludeExponent(true)
     .build()

Implementation Notes

Half-even rounding is used in cases where the decimal value must be rounded in order to meet the configuration requirements of the formatter instance.

Since
1.10.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract static class
DoubleFormat.AbstractDoubleFormat

Base class for standard double formatting classes.

public static class
DoubleFormat.Builder

Builds configured format functions for standard double format types.

private static class
DoubleFormat.EngineeringDoubleFormat

Format class that uses engineering notation for all values.

private static class
DoubleFormat.MixedDoubleFormat

Format class producing results similar to Double#toString(), with plain decimal notation for small numbers relatively close to zero and scientific notation otherwise.

private static class
DoubleFormat.PlainDoubleFormat

Format class that produces plain decimal strings that do not use scientific notation.

private static class
DoubleFormat.ScientificDoubleFormat

Format class that uses scientific notation for all values.

Field Summary

Modifier and TypeField and Description
public static final DoubleFormat
ENGINEERING

Number format similar to scientific format but adjusted so that the exponent value is always a multiple of 3, allowing easier alignment with SI prefixes.

private final Function<DoubleFormat.Builder, DoubleFunction<String>>
factory

Function used to construct instances for this format type.

public static final DoubleFormat
MIXED

Number format that uses plain format for small numbers and scientific format for large numbers.

public static final DoubleFormat
PLAIN

Number format without exponents.

public static final DoubleFormat
SCIENTIFIC

Number format that uses exponents and contains a single digit to the left of the decimal point.

Constructor Summary

AccessConstructor and Description
private
DoubleFormat(final Function<DoubleFormat.Builder, DoubleFunction<String>>
function used to construct format instances
factory
)

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public DoubleFormat.Builder

Returns:

builder instance
builder
()

Creates a Builder for building formatter functions for this format type.

public static DoubleFormat
public static DoubleFormat[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf