Class Format

  • java.lang.Object
    • at.ofai.music.util.Format


  • public class Format
    extends java.lang.Object
    A simple utility class for easier formatted output of numeric data. Formatting is controlled by static objects, so that number widths and precisions can be set once, and used multiple times.
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      protected static java.text.NumberFormat doubleFormat
      The object which performs formatting of doubles
      protected static java.text.NumberFormat intFormat
      The object which performs formatting of integers
      protected static char plusSign
      The preferred notation for positive numbers (default is no '+' sign)
    • Constructor Summary

      Constructors 
      Constructor and Description
      Format() 
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static java.lang.String d(double n)
      Convert a double to a String.
      static java.lang.String d(double n, int fd)
      Convert a double to a String with a set number of decimal places
      static java.lang.String d(double n, int id, int fd)
      Convert a double to a String with a set number of decimal places and padding to the desired minimum number of characters before the decimal point.
      static java.lang.String i(int n)
      Convert an integer to a String with padding to the desired minimum width
      static java.lang.String i(int n, int id)
      Convert an integer to a String with padding to the desired minimum width
      static void init(int id, int did, int dfd, boolean grouping)
      Initialise the formatting objects with the desired settings.
      static void matlab(double[] data, java.lang.String name)
      Output an array to file as an assignment statement for input to Matlab
      static void matlab(double[] data, java.lang.String name, int dp)
      Output an array to file as an assignment statement for input to Matlab
      static void matlab(double[] data, java.lang.String name, java.io.PrintStream out)
      Output an array to a printstream as an assignment statement for input to Matlab
      static void setGroupingUsed(boolean flag)
      Set whether digits should be grouped in 3's as in 12,000,000.
      static void setIntDigits(int dp)
      Set the number of digits for displaying integers.
      static void setPlusSign(char c)
      Sets the initial character for positive numbers (usually blank or '+')
      static void setPostDigits(int dp)
      Set the number of digits to appear after the decimal point
      static void setPreDigits(int dp)
      Set the number of digits to appear before the decimal point.
      • Methods inherited from class java.lang.Object

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

      • doubleFormat

        protected static java.text.NumberFormat doubleFormat
        The object which performs formatting of doubles
      • intFormat

        protected static java.text.NumberFormat intFormat
        The object which performs formatting of integers
      • plusSign

        protected static char plusSign
        The preferred notation for positive numbers (default is no '+' sign)
    • Constructor Detail

      • Format

        public Format()
    • Method Detail

      • setPostDigits

        public static void setPostDigits(int dp)
        Set the number of digits to appear after the decimal point
        Parameters:
        dp - the number of characters after the decimal point
      • setPreDigits

        public static void setPreDigits(int dp)
        Set the number of digits to appear before the decimal point. If the number does not require this many digits, it will be padded with spaces.
        Parameters:
        dp - the number of characters before the decimal point
      • setIntDigits

        public static void setIntDigits(int dp)
        Set the number of digits for displaying integers. If the number does not require this many digits, it will be padded with spaces.
        Parameters:
        dp - the number of digits for displaying integers
      • setGroupingUsed

        public static void setGroupingUsed(boolean flag)
        Set whether digits should be grouped in 3's as in 12,000,000.
        Parameters:
        flag - true if grouping should be used, false if not
      • setPlusSign

        public static void setPlusSign(char c)
        Sets the initial character for positive numbers (usually blank or '+')
        Parameters:
        c - the character to prefix to positive numbers
      • init

        public static void init(int id,
                                int did,
                                int dfd,
                                boolean grouping)
        Initialise the formatting objects with the desired settings.
        Parameters:
        id - the number of characters for displaying integers
        did - the number of characters before the decimal point
        dfd - the number of characters after the decimal point
        grouping - true if grouping should be used, false if not
      • d

        public static java.lang.String d(double n,
                                         int id,
                                         int fd)
        Convert a double to a String with a set number of decimal places and padding to the desired minimum number of characters before the decimal point.
        Parameters:
        n - the number to convert
        id - the number of characters before the decimal point
        fd - the number of characters after the decimal point
      • d

        public static java.lang.String d(double n,
                                         int fd)
        Convert a double to a String with a set number of decimal places
        Parameters:
        n - the number to convert
        fd - the number of characters after the decimal point
      • d

        public static java.lang.String d(double n)
        Convert a double to a String. The number of decimal places and characters before the decimal point are stored in the static members of this class.
        Parameters:
        n - the number to convert
      • i

        public static java.lang.String i(int n,
                                         int id)
        Convert an integer to a String with padding to the desired minimum width
        Parameters:
        n - the number to convert
        id - the desired minimum width
      • i

        public static java.lang.String i(int n)
        Convert an integer to a String with padding to the desired minimum width
        Parameters:
        n - the number to convert
      • matlab

        public static void matlab(double[] data,
                                  java.lang.String name)
        Output an array to file as an assignment statement for input to Matlab
        Parameters:
        data - the values to print to 4 decimal places
        name - the variable name in the Matlab assignment statement; the file name is the same name with ".m" appended, or standard out if unable to open this file
      • matlab

        public static void matlab(double[] data,
                                  java.lang.String name,
                                  int dp)
        Output an array to file as an assignment statement for input to Matlab
        Parameters:
        data - the values to print
        name - the variable name in the Matlab assignment statement; the file name is the same name with ".m" appended, or standard out if unable to open this file
        dp - the number of decimal places to print
      • matlab

        public static void matlab(double[] data,
                                  java.lang.String name,
                                  java.io.PrintStream out)
        Output an array to a printstream as an assignment statement for input to Matlab
        Parameters:
        data - the values to print
        name - the variable name in the Matlab assignment statement
        out - the output stream to print to