Class Formatter

Object
Formatter

public class Formatter extends Object
Collection of convenience methods for formatting numbers, vectors and matrices as Strings.
Author:
Christoph Hauert
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Delimiter between matrix rows.
    private static final int
    The maximum number of decimal digits to format.
    private static final DecimalFormat[]
    Array of formatters with fixed number of digits ranging from 0 to 12.
    private static final DecimalFormat[]
    Array of formatters with 0 to up to 12 non-zero decimal digits.
    private static final DecimalFormat[]
    Array of percentage formatters with fixed number of digits ranging from 0 to 12.
    private static final DecimalFormat[]
    Array of scientific formatters with fixed number of digits ranging from 0 to up to 12 non-zero decimal digits.
    private static final double[][]
    The thresholds for 'pretty' formatting of exponents for each number of digits.
    static final String
    Delimiter between vector elements.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Ensure non-instantiability with private default constructor
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    format(double[][] aMatrix, int digits)
    Format array/matrix of doubles aMatrix as String.
    static String
    format(double[] aVector, int digits)
    Format array/vector of doubles aVector as String.
    static String
    format(double aDouble, int digits)
    Format double aDouble as String with at most digits decimal places (trailing zeroes are suppressed).
    static String
    format(float[][] aMatrix, int digits)
    Format array/matrix of floats aMatrix as String.
    static String
    format(float[] aVector, int digits)
    Format array/vector of floats aVector as String.
    static String
    format(int anInteger)
    Format integer anInteger as String
    static String
    format(int[] aVector)
    Format integer array/vector aVector as String.
    static String
    format(int[][] aMatrix)
    Format array/matrix of integers aMatrix as String.
    static <T> String
    format(T[] aVector)
    Format the vector aVector of type T as String with elements separated by '", "'.
    static <T> String
    format(T[] aVector, String delimiter)
    Format the vector aVector of type T as a String with elements separated by delimiter string.
    static String
    formatFix(double[][] aMatrix, int digits)
    Format array/matrix of doubles aMatrix as String.
    static String
    formatFix(double[] aVector, int digits)
    Format array/vector of doubles aVector as String.
    static String
    formatFix(double aDouble, int digits)
    Format double aDouble as String with digits decimal places (trailing zeroes are included).
    static String
    formatPercent(double aDouble, int digits)
    Format double aDouble as percent String with digits decimal places (trailing zeroes are included).
    static String
    formatSci(double[][] aMatrix, int digits)
    Format array/matrix of doubles aMatrix as String.
    static String
    formatSci(double[] aVector, int digits)
    Format array/vector of doubles aVector as String.
    static String
    formatSci(double aDouble, int digits)
    Format double aDouble as String with up to digits decimal places (trailing zeroes are suppressed) forcing scientific formatting including exponent (separated by 'E').
    static String
    pretty(double[][] aMatrix, int digits)
    Same as formatSci(double[], int) but formatting of exponents 'prettyfied' using HTML.
    static String
    pretty(double[] aVector, int digits)
    Same as formatSci(double[], int) but formatting of exponents 'prettyfied' using HTML.
    static String
    pretty(double aDouble, int digits)
    Same as formatSci(double, int) but formatting of exponent 'prettyfied' using HTML.
    private static String
    prettyFormat(double aDouble, int digits)
    Helper method to format a double aDouble as HTML string with digits decimal places (trailing zeroes are included).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_DIGITS

      private static final int MAX_DIGITS
      The maximum number of decimal digits to format.
      See Also:
    • MATRIX_DELIMITER

      public static final String MATRIX_DELIMITER
      Delimiter between matrix rows. Note: keep in sync with CLOParser.MATRIX_DELIMITER
      See Also:
    • VECTOR_DELIMITER

      public static final String VECTOR_DELIMITER
      Delimiter between vector elements. Note: keep in sync with CLOParser.VECTOR_DELIMITER
      See Also:
    • myFormatters

      private static final DecimalFormat[] myFormatters
      Array of formatters with 0 to up to 12 non-zero decimal digits.
    • myFixFormatters

      private static final DecimalFormat[] myFixFormatters
      Array of formatters with fixed number of digits ranging from 0 to 12.
    • mySciFormatters

      private static final DecimalFormat[] mySciFormatters
      Array of scientific formatters with fixed number of digits ranging from 0 to up to 12 non-zero decimal digits.
    • myPercentFormatters

      private static final DecimalFormat[] myPercentFormatters
      Array of percentage formatters with fixed number of digits ranging from 0 to 12.
    • prettyThresholds

      private static final double[][] prettyThresholds
      The thresholds for 'pretty' formatting of exponents for each number of digits. For absolute values between the upper and lower bound fixed formatting is used, otherwise scientific formatting is applied.
  • Constructor Details

    • Formatter

      private Formatter()
      Ensure non-instantiability with private default constructor
  • Method Details

    • format

      public static <T> String format(T[] aVector)
      Format the vector aVector of type T as String with elements separated by '", "'.
      Type Parameters:
      T - the type of the vector
      Parameters:
      aVector - the vector to format
      Returns:
      the formatted String vector
    • format

      public static <T> String format(T[] aVector, String delimiter)
      Format the vector aVector of type T as a String with elements separated by delimiter string.
      Type Parameters:
      T - the type of the vector
      Parameters:
      aVector - the string vector to format
      delimiter - the delimiter for separating the entries in aVector
      Returns:
      the formatted String vector
    • format

      public static String format(int anInteger)
      Format integer anInteger as String
      Parameters:
      anInteger - number to format
      Returns:
      formatted int as String
    • format

      public static String format(int[] aVector)
      Format integer array/vector aVector as String. Elements are separated by '", "'.
      Parameters:
      aVector - array to format
      Returns:
      formatted int[] as String
    • format

      public static String format(int[][] aMatrix)
      Format array/matrix of integers aMatrix as String. Column elements are separated by '", "' and rows of elements by '"; "'.
      Parameters:
      aMatrix - array to format
      Returns:
      formatted int[][] as String
    • format

      public static String format(double aDouble, int digits)
      Format double aDouble as String with at most digits decimal places (trailing zeroes are suppressed).
      Parameters:
      aDouble - number to format
      digits - number of decimal places
      Returns:
      formatted double as String
    • format

      public static String format(double[] aVector, int digits)
      Format array/vector of doubles aVector as String. Elements are separated by '", "' and formatted with at most digits decimal places (trailing zeroes are suppressed).
      Parameters:
      aVector - array to format
      digits - number of decimal places
      Returns:
      formatted double[] as String
    • format

      public static String format(double[][] aMatrix, int digits)
      Format array/matrix of doubles aMatrix as String. Column elements are separated by '", "' and rows of elements by '"; "'. Each element is formatted with at most digits decimal places (trailing zeroes are suppressed).
      Parameters:
      aMatrix - array to format
      digits - number of decimal places
      Returns:
      formatted double[][] as String
    • format

      public static String format(float[] aVector, int digits)
      Format array/vector of floats aVector as String. Elements are separated by '", "' and formatted with at most digits decimal places (trailing zeroes are suppressed).
      Parameters:
      aVector - array to format
      digits - number of decimal places
      Returns:
      formatted float[] as String
    • format

      public static String format(float[][] aMatrix, int digits)
      Format array/matrix of floats aMatrix as String. Column elements are separated by '", "' and rows of elements by '"; "'. Each element is formatted with at most digits decimal places (trailing zeroes are suppressed).
      Parameters:
      aMatrix - array to format
      digits - number of decimal places
      Returns:
      formatted float[][] as String
    • formatFix

      public static String formatFix(double aDouble, int digits)
      Format double aDouble as String with digits decimal places (trailing zeroes are included).
      Parameters:
      aDouble - number to format
      digits - number of decimal places
      Returns:
      formatted double as String
    • formatFix

      public static String formatFix(double[] aVector, int digits)
      Format array/vector of doubles aVector as String. Elements are separated by '", "' and formatted with digits decimal places (trailing zeroes are included).
      Parameters:
      aVector - array to format
      digits - number of decimal places
      Returns:
      formatted double[] as String
    • formatFix

      public static String formatFix(double[][] aMatrix, int digits)
      Format array/matrix of doubles aMatrix as String. Column elements are separated by '", "' and rows of elements by '"; "'. Each element is formatted with digits decimal places (trailing zeroes are included).
      Parameters:
      aMatrix - array to format
      digits - number of decimal places
      Returns:
      formatted double[][] as String
    • formatSci

      public static String formatSci(double aDouble, int digits)
      Format double aDouble as String with up to digits decimal places (trailing zeroes are suppressed) forcing scientific formatting including exponent (separated by 'E').
      Parameters:
      aDouble - number to format
      digits - number of decimal places
      Returns:
      formatted double as String
    • formatSci

      public static String formatSci(double[] aVector, int digits)
      Format array/vector of doubles aVector as String. Elements are separated by '", "' and formatted with up to digits decimal places (trailing zeroes are suppressed) forcing scientific formatting including exponent (separated by 'E').
      Parameters:
      aVector - array to format
      digits - number of decimal places
      Returns:
      formatted double[] as String
    • formatSci

      public static String formatSci(double[][] aMatrix, int digits)
      Format array/matrix of doubles aMatrix as String. Column elements are separated by '", "' and rows of elements by '"; "'. Each element is formatted with up to digits decimal places (trailing zeroes are suppressed) forcing scientific formatting including exponent (separated by 'E').
      Parameters:
      aMatrix - array to format
      digits - number of decimal places
      Returns:
      formatted double[][] as String
    • prettyFormat

      private static String prettyFormat(double aDouble, int digits)
      Helper method to format a double aDouble as HTML string with digits decimal places (trailing zeroes are included).
      Parameters:
      aDouble - the number to format
      digits - the number of decimal places
      Returns:
      the formatted double as HTML string
    • pretty

      public static String pretty(double aDouble, int digits)
      Same as formatSci(double, int) but formatting of exponent 'prettyfied' using HTML.
      Parameters:
      aDouble - number to format
      digits - number of decimal places
      Returns:
      formatted double as HTML string
    • pretty

      public static String pretty(double[] aVector, int digits)
      Same as formatSci(double[], int) but formatting of exponents 'prettyfied' using HTML.
      Parameters:
      aVector - number to format
      digits - number of decimal places
      Returns:
      formatted double[] as HTML string
    • pretty

      public static String pretty(double[][] aMatrix, int digits)
      Same as formatSci(double[], int) but formatting of exponents 'prettyfied' using HTML.
      Parameters:
      aMatrix - number to format
      digits - number of decimal places
      Returns:
      formatted double[][] as HTML string
    • formatPercent

      public static String formatPercent(double aDouble, int digits)
      Format double aDouble as percent String with digits decimal places (trailing zeroes are included).
      Parameters:
      aDouble - number to format
      digits - number of decimal places
      Returns:
      formatted double as String