Enum DoubleFormat

  • All Implemented Interfaces:
    Serializable, Comparable<DoubleFormat>

    public enum DoubleFormat
    extends Enum<DoubleFormat>
    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 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.

    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
    • Method Detail

      • values

        public static DoubleFormat[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (DoubleFormat c : DoubleFormat.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static DoubleFormat valueOf​(String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        IllegalArgumentException - if this enum type has no constant with the specified name
        NullPointerException - if the argument is null