Class FloatFormatter

  • All Implemented Interfaces:
    java.lang.Appendable

    public class FloatFormatter
    extends InternalFormat.Formatter
    A class that provides the implementation of floating-point formatting. In a limited way, it acts like a StringBuilder to which text and one or more numbers may be appended, formatted according to the format specifier supplied at construction. These are ephemeral objects that are not, on their own, thread safe.
    • Constructor Detail

      • FloatFormatter

        public FloatFormatter​(java.lang.StringBuilder result,
                              InternalFormat.Spec spec)
        Construct the formatter from a client-supplied buffer, to which the result will be appended, and a specification. Sets InternalFormat.Formatter.mark to the end of the buffer.
        Parameters:
        result - destination buffer
        spec - parsed conversion specification
      • FloatFormatter

        public FloatFormatter​(InternalFormat.Spec spec)
        Construct the formatter from a specification, allocating a buffer internally for the result.
        Parameters:
        spec - parsed conversion specification
    • Method Detail

      • size

        public static int size​(InternalFormat.Spec spec)
        Recommend a buffer size for a given specification, assuming one float is converted. This will be a "right" answer for e and g-format, and for f-format with values up to 9,999,999.
        Parameters:
        spec - parsed conversion specification
      • setMinFracDigits

        public void setMinFracDigits​(int minFracDigits)
        Override the default truncation behaviour for the specification originally supplied. Some formats remove trailing zero digits, trimming to zero or one. Set member minFracDigits, to modify this behaviour.
        Parameters:
        minFracDigits - if <0 prevent truncation; if >=0 the minimum number of fractional digits; when this is zero, and all fractional digits are zero, the decimal point will also be removed.
      • append

        public FloatFormatter append​(java.lang.CharSequence csq,
                                     int start,
                                     int end)
                              throws java.lang.IndexOutOfBoundsException
        Specified by:
        append in interface java.lang.Appendable
        Overrides:
        append in class InternalFormat.Formatter
        Throws:
        java.lang.IndexOutOfBoundsException
      • format

        public FloatFormatter format​(double value)
        Format a floating-point number according to the specification represented by this FloatFormatter.
        Parameters:
        value - to convert
        Returns:
        this object
      • format

        public FloatFormatter format​(double value,
                                     java.lang.String positivePrefix)
        Format a floating-point number according to the specification represented by this FloatFormatter. The conversion type, precision, and flags for grouping or percentage are dealt with here. At the point this is used, we know the InternalFormat.Formatter.spec is one of the floating-point types. This entry point allows explicit control of the prefix of positive numbers, overriding defaults for the format type.
        Parameters:
        value - to convert
        positivePrefix - to use before positive values (e.g. "+") or null to default to ""
        Returns:
        this object