Chris@2: /* Chris@2: Copyright (C) 2001, 2006 by Simon Dixon Chris@2: Chris@2: This program is free software; you can redistribute it and/or modify Chris@2: it under the terms of the GNU General Public License as published by Chris@2: the Free Software Foundation; either version 2 of the License, or Chris@2: (at your option) any later version. Chris@2: Chris@2: This program is distributed in the hope that it will be useful, Chris@2: but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@2: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@2: GNU General Public License for more details. Chris@2: Chris@2: You should have received a copy of the GNU General Public License along Chris@2: with this program (the file gpl.txt); if not, download it from Chris@2: http://www.gnu.org/licenses/gpl.txt or write to the Chris@2: Free Software Foundation, Inc., Chris@2: 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Chris@2: */ Chris@2: Chris@2: package at.ofai.music.util; Chris@2: Chris@2: import java.text.NumberFormat; Chris@2: import java.io.*; Chris@2: Chris@2: /** A simple utility class for easier formatted output of numeric data. Chris@2: * Formatting is controlled by static objects, so that number widths and Chris@2: * precisions can be set once, and used multiple times. Chris@2: */ Chris@2: public class Format { Chris@2: Chris@2: /** The object which performs formatting of integers */ Chris@2: protected static NumberFormat intFormat = NumberFormat.getInstance(); Chris@2: Chris@2: /** The object which performs formatting of doubles */ Chris@2: protected static NumberFormat doubleFormat = NumberFormat.getInstance(); Chris@2: Chris@2: /** The preferred notation for positive numbers (default is no '+' sign) */ Chris@2: protected static char plusSign = ' '; Chris@2: Chris@2: /** Set the number of digits to appear after the decimal point Chris@2: * @param dp the number of characters after the decimal point Chris@2: */ Chris@2: public static void setPostDigits(int dp) { Chris@2: doubleFormat.setMinimumFractionDigits(dp); Chris@2: doubleFormat.setMaximumFractionDigits(dp); Chris@2: } // setPostDigits() Chris@2: Chris@2: /** Set the number of digits to appear before the decimal point. Chris@2: * If the number does not require this many digits, it will be Chris@2: * padded with spaces. Chris@2: * @param dp the number of characters before the decimal point Chris@2: */ Chris@2: public static void setPreDigits(int dp) { Chris@2: doubleFormat.setMinimumIntegerDigits(dp); Chris@2: } // setPreDigits() Chris@2: Chris@2: /** Set the number of digits for displaying integers. Chris@2: * If the number does not require this many digits, it will be Chris@2: * padded with spaces. Chris@2: * @param dp the number of digits for displaying integers Chris@2: */ Chris@2: public static void setIntDigits(int dp) { Chris@2: intFormat.setMinimumIntegerDigits(dp); Chris@2: intFormat.setMinimumFractionDigits(0); Chris@2: intFormat.setMaximumFractionDigits(0); Chris@2: } // setIntegerDigits() Chris@2: Chris@2: /** Set whether digits should be grouped in 3's as in 12,000,000. Chris@2: * @param flag true if grouping should be used, false if not Chris@2: */ Chris@2: public static void setGroupingUsed(boolean flag) { Chris@2: doubleFormat.setGroupingUsed(flag); Chris@2: intFormat.setGroupingUsed(flag); Chris@2: } // setGroupingUsed() Chris@2: Chris@2: /** Sets the initial character for positive numbers (usually blank or '+') Chris@2: * @param c the character to prefix to positive numbers Chris@2: */ Chris@2: public static void setPlusSign(char c) { Chris@2: plusSign = c; Chris@2: } // setPlusSign() Chris@2: Chris@2: /** Initialise the formatting objects with the desired settings. Chris@2: * @param id the number of characters for displaying integers Chris@2: * @param did the number of characters before the decimal point Chris@2: * @param dfd the number of characters after the decimal point Chris@2: * @param grouping true if grouping should be used, false if not Chris@2: */ Chris@2: public static void init(int id, int did, int dfd, boolean grouping) { Chris@2: setIntDigits(id); Chris@2: setPreDigits(did); Chris@2: setPostDigits(dfd); Chris@2: setGroupingUsed(grouping); Chris@2: } // init() Chris@2: Chris@2: /** Convert a double to a String with a set number of decimal places and Chris@2: * padding to the desired minimum number of characters before the decimal Chris@2: * point. Chris@2: * @param n the number to convert Chris@2: * @param id the number of characters before the decimal point Chris@2: * @param fd the number of characters after the decimal point Chris@2: */ Chris@2: public static String d(double n, int id, int fd) { Chris@2: setPreDigits(id); Chris@2: return d(n, fd); Chris@2: } // d() Chris@2: Chris@2: /** Convert a double to a String with a set number of decimal places Chris@2: * @param n the number to convert Chris@2: * @param fd the number of characters after the decimal point Chris@2: */ Chris@2: public static String d(double n, int fd) { Chris@2: setPostDigits(fd); Chris@2: return d(n); Chris@2: } // d() Chris@2: Chris@2: /** Convert a double to a String. The number of decimal places and Chris@2: * characters before the decimal point are stored in the static members Chris@2: * of this class. Chris@2: * @param n the number to convert Chris@2: */ Chris@2: public static String d(double n) { Chris@2: String s; Chris@2: if (Double.isNaN(n)) Chris@2: return "NaN"; Chris@2: s = doubleFormat.format(n); Chris@2: if (n >= 0) Chris@2: s = plusSign + s; Chris@2: char[] c = s.toCharArray(); Chris@2: int i; Chris@2: for (i = 1; (i < c.length-1) && (c[i] == '0') && (c[i+1] != '.'); i++) { Chris@2: c[i] = c[i-1]; Chris@2: c[i-1] = ' '; Chris@2: } Chris@2: if (i > 1) Chris@2: s = new String(c); Chris@2: return s; Chris@2: } // d() Chris@2: Chris@2: /** Convert an integer to a String with padding to the desired minimum width Chris@2: * @param n the number to convert Chris@2: * @param id the desired minimum width Chris@2: */ Chris@2: public static String i(int n, int id) { Chris@2: setIntDigits(id); Chris@2: return i(n); Chris@2: } // i() Chris@2: Chris@2: /** Convert an integer to a String with padding to the desired minimum width Chris@2: * @param n the number to convert Chris@2: */ Chris@2: public static String i(int n) { Chris@2: return (n < 0)? intFormat.format(n): plusSign+intFormat.format(n); Chris@2: } // i() Chris@2: Chris@2: /** Output an array to file as an assignment statement for input to Matlab Chris@2: * @param data the values to print to 4 decimal places Chris@2: * @param name the variable name in the Matlab assignment statement; the Chris@2: * file name is the same name with ".m" appended, or standard out if Chris@2: * unable to open this file Chris@2: */ Chris@2: public static void matlab(double[] data, String name) { Chris@2: matlab(data, name, 4); Chris@2: } // matlab() Chris@2: Chris@2: /** Output an array to file as an assignment statement for input to Matlab Chris@2: * @param data the values to print Chris@2: * @param name the variable name in the Matlab assignment statement; the Chris@2: * file name is the same name with ".m" appended, or standard out if Chris@2: * unable to open this file Chris@2: * @param dp the number of decimal places to print Chris@2: */ Chris@2: public static void matlab(double[] data, String name, int dp) { Chris@2: setPostDigits(dp); Chris@2: PrintStream out; Chris@2: try { Chris@2: out = new PrintStream(new FileOutputStream(name+".m")); Chris@2: } catch (FileNotFoundException e) { Chris@2: out = System.out; Chris@2: } Chris@2: matlab(data, name, out); Chris@2: if (out != System.out) Chris@2: out.close(); Chris@2: } // matlab Chris@2: Chris@2: /** Output an array to a printstream as an assignment statement for input to Chris@2: * Matlab Chris@2: * @param data the values to print Chris@2: * @param name the variable name in the Matlab assignment statement Chris@2: * @param out the output stream to print to Chris@2: */ Chris@2: public static void matlab(double[] data, String name, PrintStream out) { Chris@2: out.println(name + " = ["); Chris@2: setGroupingUsed(false); Chris@2: for (int i=0; i < data.length; i++) Chris@2: out.println(d(data[i])); Chris@2: out.println("];"); Chris@2: } // matlab() Chris@2: Chris@2: } // class Format