Class NumberFormatter

java.lang.Object
org.moeaframework.util.format.NumberFormatter
All Implemented Interfaces:
Formatter<Number>

public class NumberFormatter extends Object implements Formatter<Number>
Formatter for displaying numeric data. This supports all primitive numeric types as well as their boxed types.
  • Constructor Details

    • NumberFormatter

      public NumberFormatter()
      Creates a new formatter for numeric types with default settings.
  • Method Details

    • getDefault

      public static NumberFormatter getDefault()
      Returns the default number formatter. Changes made to this formatter will be reflected everywhere the default formatter is used.
      Returns:
      the default number formatter
    • setDefault

      public static void setDefault(NumberFormatter formatter)
      Sets the default number formatter.
      Parameters:
      formatter - the formatter to use as the default; if null, resets to the default settings.
    • getType

      public Class<Number> getType()
      Description copied from interface: Formatter
      Returns the class (or classes using inheritance) this formatter supports.
      Specified by:
      getType in interface Formatter<Number>
      Returns:
      the class
    • getWidth

      public int getWidth()
      Returns the width of this field.
      Returns:
      the width
    • setWidth

      public void setWidth(int width)
      Sets the width of this field. See String.format(String, Object...) for details.
      Parameters:
      width - the width
    • getPrecision

      public int getPrecision()
      Returns the precision of this field.
      Returns:
      the precision
    • setPrecision

      public void setPrecision(int precision)
      Sets the precision of this field. See String.format(String, Object...) for details.
      Parameters:
      precision - the precision
    • isLeadingSpaceForSign

      public boolean isLeadingSpaceForSign()
      Returns true if this format reserves a leading space for the sign.
      Returns:
      true if this format reserves a leading space for the sign; false otherwise
    • setLeadingSpaceForSign

      public void setLeadingSpaceForSign(boolean leadingSpaceForSign)
      Set to true to reserve a leading space for the sign; false otherwise. This is equivalent to using the "-" format option. See String.format(String, Object...) for details.
      Parameters:
      leadingSpaceForSign - true to reserve a leading space for the sign; false otherwise
    • isLocaleSpecificGroupings

      public boolean isLocaleSpecificGroupings()
      Returns true if this formatting uses locale-specific groupings.
      Returns:
      true if this formatting uses locale-specific groupings; false otherwise
    • setLocaleSpecificGroupings

      public void setLocaleSpecificGroupings(boolean localeSpecificGroupings)
      Set to true to use locale-specific groupings. For example, this may include adding a comma or other character to split thousands-places.
      Parameters:
      localeSpecificGroupings - true if this formatting uses locale-specific groupings; false otherwise
    • isScientificNotation

      public boolean isScientificNotation()
      Returns true if this formatting uses scientific notation.
      Returns:
      true if this formatting uses scientific notation; false otherwise
    • setScientificNotation

      public void setScientificNotation(boolean scientificNotation)
      Set to true to use scientific notation. When enabled, the number will include the exponent and is useful when expressing very small or large numbers.
      Parameters:
      scientificNotation - true if this formatting uses scientific notation; false otherwise
    • getLocale

      public Locale getLocale()
      Returns the locale used when formatting the data.
      Returns:
      the locale used when formatting the data
    • setLocale

      public void setLocale(Locale locale)
      Sets the locale used when formatting the data. If unset, the default system locale is used.
      Parameters:
      locale - the new locale
    • createDecimalFormatString

      protected String createDecimalFormatString()
      Creates the formatting string used by String.format(String, Object...) to display decimal values.
      Returns:
      the formatting string
    • createIntegerFormatString

      protected String createIntegerFormatString()
      Creates the formatting string used by String.format(String, Object...) to display integer values.
      Returns:
      the formatting string
    • format

      public String format(Object value)
      Description copied from interface: Formatter
      Formats the given value into a string representation.
      Specified by:
      format in interface Formatter<Number>
      Parameters:
      value - the value
      Returns:
      the string representation
    • format

      public String format(double[] values)
      Formats an array of double values into a string representation.
      Parameters:
      values - the values
      Returns:
      the string representation
    • format

      public String format(int[] values)
      Formats an array of integer values into a string representation.
      Parameters:
      values - the values
      Returns:
      the string representation