Class FastDoubleFormatter


  • public class FastDoubleFormatter
    extends Object
    Formatter for double numbers with low overhead.

    This class is intended to be used when formatting large amounts of data with fixed formats like, for example, large ephemeris or measurement files.

    Building the formatter is done once, and the formatter appendTo(Appendable, double) or toString(double) methods can be called hundreds of thousands of times, without incurring the overhead that would occur with String.format(). Some tests showed this formatter is about 5 times faster than String.format() with %{width}.{%precision}f format.

    Instances of this class are immutable

    Since:
    13.0.3
    Author:
    Luc Maisonobe
    • Constructor Detail

      • FastDoubleFormatter

        public FastDoubleFormatter​(int width,
                                   int precision)
        Simple constructor.

        This constructor is equivalent to Formatter float format %{width}.{precision}f

        Parameters:
        width - number of characters to output
        precision - number of decimal precision
    • Method Detail

      • getWidth

        public int getWidth()
        Get the width.
        Returns:
        width
      • getPrecision

        public int getPrecision()
        Get the precision.
        Returns:
        precision
      • appendTo

        public void appendTo​(Appendable appendable,
                             double value)
                      throws IOException
        Append one formatted value to an Appendable.
        Parameters:
        appendable - to append value to
        value - value to format
        Throws:
        IOException - if an I/O error occurs
      • toString

        public String toString​(double value)
        Format one value.
        Parameters:
        value - value to format
        Returns:
        formatted string