Java Locale Format formatTo(StringBuilder buf, int[] d, String sep)

Here you can find the source of formatTo(StringBuilder buf, int[] d, String sep)

Description

Formats the int array d.

License

Open Source License

Parameter

Parameter Description
buf String builder to append to
d the int array to be formatted
sep separator between the single values of the array, e.g. ','

Return

Output buffer buf

Declaration

public static StringBuilder formatTo(StringBuilder buf, int[] d, String sep) 

Method Source Code


//package com.java2s;
/*/* www .ja  va  2 s  .c o m*/
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures
    
 Copyright (C) 2015
 Ludwig-Maximilians-Universit?t M?nchen
 Lehr- und Forschungseinheit f?r Datenbanksysteme
 ELKI Development Team
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.
    
 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;

import java.util.Collection;

import java.util.Iterator;

import java.util.Locale;

public class Main {
    /**
     * Number Formatter (2 digits) for output purposes.
     */
    public static final NumberFormat NF2 = NumberFormat.getInstance(Locale.US);
    /**
     * Whitespace. The string should cover the commonly used length.
     */
    private static final String WHITESPACE_BUFFER = "                                                                                ";
    /**
     * Length of the whitespace buffer.
     */
    private static final int WHITESPACE_BUFFER_LENGTH = WHITESPACE_BUFFER.length();

    /**
     * Formats the double array d with the default number format.
     *
     * @param buf String builder to append to
     * @param d the double array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @return Output buffer buf
     */
    public static StringBuilder formatTo(StringBuilder buf, double[] d, String sep) {
        if (d == null) {
            return buf.append("null");
        }
        if (d.length == 0) {
            return buf;
        }
        buf.append(d[0]);
        for (int i = 1; i < d.length; i++) {
            buf.append(sep);
            buf.append(d[i]);
        }
        return buf;
    }

    /**
     * Formats the double array d with the specified number format.
     *
     * @param buf String builder to append to
     * @param d the double array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @param nf the number format to be used for formatting
     * @return Output buffer buf
     */
    public static StringBuilder formatTo(StringBuilder buf, double[] d, String sep, NumberFormat nf) {
        if (d == null) {
            return buf.append("null");
        }
        if (d.length == 0) {
            return buf;
        }
        buf.append(nf.format(d[0]));
        for (int i = 1; i < d.length; i++) {
            buf.append(sep);
            buf.append(nf.format(d[i]));
        }
        return buf;
    }

    /**
     * Formats the float array d with the default number format.
     *
     * @param buf String builder to append to
     * @param d the float array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @return Output buffer buf
     */
    public static StringBuilder formatTo(StringBuilder buf, float[] d, String sep) {
        if (d == null) {
            return buf.append("null");
        }
        if (d.length == 0) {
            return buf;
        }
        buf.append(d[0]);
        for (int i = 1; i < d.length; i++) {
            buf.append(sep);
            buf.append(d[i]);
        }
        return buf;
    }

    /**
     * Formats the float array d with the specified number format.
     *
     * @param buf String builder to append to
     * @param d the float array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @param nf the number format to be used for formatting
     * @return Output buffer buf
     */
    public static StringBuilder formatTo(StringBuilder buf, float[] d, String sep, NumberFormat nf) {
        if (d == null) {
            return buf.append("null");
        }
        if (d.length == 0) {
            return buf;
        }
        buf.append(nf.format(d[0]));
        for (int i = 1; i < d.length; i++) {
            buf.append(sep);
            buf.append(nf.format(d[i]));
        }
        return buf;
    }

    /**
     * Formats the int array d.
     *
     * @param buf String builder to append to
     * @param d the int array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @return Output buffer buf
     */
    public static StringBuilder formatTo(StringBuilder buf, int[] d, String sep) {
        if (d == null) {
            return buf.append("null");
        }
        if (d.length == 0) {
            return buf;
        }
        buf.append(d[0]);
        for (int i = 1; i < d.length; i++) {
            buf.append(sep);
            buf.append(d[i]);
        }
        return buf;
    }

    /**
     * Formats the long array d.
     *
     * @param buf String builder to append to
     * @param d the long array to be formatted
     * @param sep separator between the single values of the long array, e.g. ','
     * @return Output buffer buf
     */
    public static StringBuilder formatTo(StringBuilder buf, long[] d, String sep) {
        if (d == null) {
            return buf.append("null");
        }
        if (d.length == 0) {
            return buf;
        }
        buf.append(d[0]);
        for (int i = 1; i < d.length; i++) {
            buf.append(sep);
            buf.append(d[i]);
        }
        return buf;
    }

    /**
     * Formats the short array d.
     *
     * @param buf String builder to append to
     * @param d the int array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @return Output buffer buf
     */
    public static StringBuilder formatTo(StringBuilder buf, short[] d, String sep) {
        if (d == null) {
            return buf.append("null");
        }
        if (d.length == 0) {
            return buf;
        }
        buf.append(d[0]);
        for (int i = 1; i < d.length; i++) {
            buf.append(sep);
            buf.append(d[i]);
        }
        return buf;
    }

    /**
     * Formats the byte array d.
     *
     * @param buf String builder to append to
     * @param d the byte array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @return Output buffer buf
     */
    public static StringBuilder formatTo(StringBuilder buf, byte[] d, String sep) {
        if (d == null) {
            return buf.append("null");
        }
        if (d.length == 0) {
            return buf;
        }
        buf.append(d[0]);
        for (int i = 1; i < d.length; i++) {
            buf.append(sep);
            buf.append(d[i]);
        }
        return buf;
    }

    /**
     * Formats the boolean array d.
     *
     * @param buf String builder to append to
     * @param d the boolean array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @return Output buffer buf
     */
    public static StringBuilder formatTo(StringBuilder buf, boolean[] d, String sep) {
        if (d == null) {
            return buf.append("null");
        }
        if (d.length == 0) {
            return buf;
        }
        buf.append(d[0]);
        for (int i = 1; i < d.length; i++) {
            buf.append(sep);
            buf.append(format(d[i]));
        }
        return buf;
    }

    /**
     * Format a boolean value as string "true" or "false".
     *
     * @param buf Buffer to append to
     * @param b Boolean to Format
     * @return Same buffer
     */
    public static StringBuilder formatTo(StringBuilder buf, boolean b) {
        return buf.append(b ? "true" : "false");
    }

    /**
     * Formats the array of double arrays d with the specified separators and
     * fraction digits.
     *
     * @param buf Output buffer
     * @param d the double array to be formatted
     * @param pre Row prefix (e.g. " [")
     * @param pos Row postfix (e.g. "]\n")
     * @param csep Separator for columns (e.g. ", ")
     * @param nf the number format to use
     * @return Output buffer buf
     */
    public static StringBuilder formatTo(StringBuilder buf, double[][] d, String pre, String pos, String csep,
            NumberFormat nf) {
        if (d == null) {
            return buf.append("null");
        }
        if (d.length == 0) {
            return buf;
        }
        for (int i = 0; i < d.length; i++) {
            buf.append(pre);
            formatTo(buf, d[i], csep, nf);
            buf.append(pos);
        }
        return buf;
    }

    /**
     * Formats the double array d with ', ' as separator and default precision.
     *
     * @param d the double array to be formatted
     * @return a String representing the double array d
     */
    public static String format(double[] d) {
        return d == null ? "null" : (d.length == 0) ? "" : //
                formatTo(new StringBuilder(), d, ", ").toString();
    }

    /**
     * Formats the double array d with the specified separator.
     *
     * @param d the double array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @return a String representing the double array d
     */
    public static String format(double[] d, String sep) {
        return d == null ? "null" : (d.length == 0) ? "" : //
                formatTo(new StringBuilder(), d, sep).toString();
    }

    /**
     * Formats the double array d with the specified number format.
     *
     * @param d the double array to be formatted
     * @param nf the number format to be used for formatting
     * @return a String representing the double array d
     */
    public static String format(double[] d, NumberFormat nf) {
        return format(d, " ", nf);
    }

    /**
     * Formats the double array d with the specified number format.
     *
     * @param d the double array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @param nf the number format to be used for formatting
     * @return a String representing the double array d
     */
    public static String format(double[] d, String sep, NumberFormat nf) {
        return d == null ? "null" : (d.length == 0) ? "" : //
                formatTo(new StringBuilder(), d, sep, nf).toString();
    }

    /**
     * Returns a string representation of this vector.
     *
     * @param w column width
     * @param d number of digits after the decimal
     * @return a string representation of this matrix
     */
    public static String format(double[] v, int w, int d) {
        DecimalFormat format = new DecimalFormat();
        format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
        format.setMinimumIntegerDigits(1);
        format.setMaximumFractionDigits(d);
        format.setMinimumFractionDigits(d);
        format.setGroupingUsed(false);

        int width = w + 1;
        StringBuilder msg = new StringBuilder();
        msg.append('\n'); // start on new line.
        for (int i = 0; i < v.length; i++) {
            String s = format.format(v[i]); // format the number
            int padding = Math.max(1, width - s.length()); // At _least_ 1
            // space
            for (int k = 0; k < padding; k++) {
                msg.append(' ');
            }
            msg.append(s);
        }
        // msg.append("\n");

        return msg.toString();
    }

    /**
     * Formats the float array d with the specified number format.
     *
     * @param d the float array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @param nf the number format to be used for formatting
     * @return a String representing the double array d
     */
    public static String format(float[] d, String sep, NumberFormat nf) {
        return (d == null) ? "null" : (d.length == 0) ? "" : //
                formatTo(new StringBuilder(), d, sep, nf).toString();
    }

    /**
     * Formats the float array d with the specified number format.
     *
     * @param d the float array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @param nf the number format to be used for formatting
     * @return a String representing the double array d
     */
    public static String format(float[] d, String sep) {
        return (d == null) ? "null" : (d.length == 0) ? "" : //
                formatTo(new StringBuilder(), d, sep).toString();
    }

    /**
     * Formats the float array f with ',' as separator and default precision.
     *
     * @param f the float array to be formatted
     * @return a String representing the float array f
     */
    public static String format(float[] f) {
        return (f == null) ? "null" : (f.length == 0) ? "" : //
                formatTo(new StringBuilder(), f, ", ").toString();
    }

    /**
     * Formats the int array a for printing purposes.
     * 
     * @param a the int array to be formatted
     * @param sep the separator between the single values of the array, e.g. ','
     * @return a String representing the int array a
     */
    public static String format(int[] a, String sep) {
        return (a == null) ? "null" : (a.length == 0) ? "" : //
                formatTo(new StringBuilder(), a, sep).toString();
    }

    /**
     * Formats the int array a for printing purposes.
     *
     * @param a the int array to be formatted
     * @return a String representing the int array a
     */
    public static String format(int[] a) {
        return (a == null) ? "null" : (a.length == 0) ? "" : //
                formatTo(new StringBuilder(), a, ", ").toString();
    }

    /**
     * Formats the long array a for printing purposes.
     *
     * @param a the long array to be formatted
     * @return a String representing the long array a
     */
    public static String format(long[] a) {
        return (a == null) ? "null" : (a.length == 0) ? "" : //
                formatTo(new StringBuilder(), a, ", ").toString();
    }

    /**
     * Formats the byte array a for printing purposes.
     *
     * @param a the byte array to be formatted
     * @return a String representing the byte array a
     */
    public static String format(byte[] a) {
        return (a == null) ? "null" : (a.length == 0) ? "" : //
                formatTo(new StringBuilder(), a, ", ").toString();
    }

    /**
     * Formats the boolean array b with ',' as separator.
     *
     * @param b the boolean array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @return a String representing the boolean array b
     */
    public static String format(boolean[] b, final String sep) {
        return (b == null) ? "null" : (b.length == 0) ? "" : //
                formatTo(new StringBuilder(), b, ", ").toString();
    }

    /**
     * Formats the boolean b.
     *
     * @param b the boolean to be formatted
     * @return a String representing of the boolean b
     */
    public static String format(final boolean b) {
        return b ? "1" : "0";
    }

    /**
     * Formats the double array d with ',' as separator and 2 fraction digits.
     *
     * @param d the double array to be formatted
     * @return a String representing the double array d
     */
    public static String format(double[][] d) {
        return d == null ? "null" : (d.length == 0) ? "[]" : //
                formatTo(new StringBuilder().append("[\n"), d, " [", "]\n", ", ", NF2).append(']').toString();
    }

    /**
     * Formats the array of double arrays d with 'the specified separators and
     * fraction digits.
     *
     * @param d the double matrix to be formatted
     * @param pre Row prefix (e.g. " [")
     * @param pos Row postfix (e.g. "]\n")
     * @param csep Separator for columns (e.g. ", ")
     * @param nf the number format to use
     * @return a String representing the double array d
     */
    public static String format(double[][] d, String pre, String pos, String csep, NumberFormat nf) {
        return d == null ? "null" : (d.length == 0) ? "" : //
                formatTo(new StringBuilder(), d, pre, pos, csep, nf).toString();
    }

    /**
     * Returns a string representation of this matrix.
     *
     * @param w column width
     * @param d number of digits after the decimal
     * @param pre Row prefix (e.g. " [")
     * @param pos Row postfix (e.g. "]\n")
     * @param csep Column separator (e.g. ", ")
     * @return a string representation of this matrix
     */
    public static String format(double[][] m, int w, int d, String pre, String pos, String csep) {
        DecimalFormat format = new DecimalFormat();
        format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
        format.setMinimumIntegerDigits(1);
        format.setMaximumFractionDigits(d);
        format.setMinimumFractionDigits(d);
        format.setGroupingUsed(false);

        StringBuilder msg = new StringBuilder();
        for (int i = 0; i < m.length; i++) {
            double[] row = m[i];
            msg.append(pre);
            for (int j = 0; j < row.length; j++) {
                if (j > 0) {
                    msg.append(csep);
                }
                String s = format.format(row[j]); // format the number
                whitespace(msg, w - s.length());
                msg.append(s);
            }
            msg.append(pos);
        }
        return msg.toString();
    }

    /**
     * Returns a string representation of this matrix. In each line the specified
     * String <code>pre</code> is prefixed.
     *
     * @param pre the prefix of each line
     * @return a string representation of this matrix
     */
    public static String format(double[][] m, String pre) {
        StringBuilder output = new StringBuilder();
        output.append(pre).append("[\n").append(pre);
        for (int i = 0; i < m.length; i++) {
            double[] row = m[i];
            output.append(" [");
            formatTo(output, row, ", ");
            output.append("]\n").append(pre);
        }
        output.append("]\n");
        return output.toString();
    }

    /**
     * returns String-representation of Matrix.
     *
     * @param nf NumberFormat to specify output precision
     * @return String representation of this Matrix in precision as specified by
     *         given NumberFormat
     */
    public static String format(double[][] m, NumberFormat nf) {
        return formatTo(new StringBuilder().append("[\n"), m, " [", "]\n", ", ", nf).append("]").toString();
    }

    /**
     * Formats the String collection with the specified separator.
     *
     * @param d the String collection to format
     * @param sep separator between the single values of the array, e.g. ' '
     * @return a String representing the String Collection d
     */
    public static String format(Collection<String> d, String sep) {
        if (d == null) {
            return "null";
        }
        if (d.isEmpty()) {
            return "";
        }
        if (d.size() == 1) {
            return d.iterator().next();
        }
        StringBuilder buffer = new StringBuilder();
        Iterator<String> it = d.iterator();
        buffer.append(it.next());
        while (it.hasNext()) {
            buffer.append(sep);
            buffer.append(it.next());
        }
        return buffer.toString();
    }

    /**
     * Formats the string array d with the specified separator.
     *
     * @param d the string array to be formatted
     * @param sep separator between the single values of the array, e.g. ','
     * @return a String representing the string array d
     */
    public static String format(String[] d, String sep) {
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < d.length; i++) {
            if (i > 0) {
                buffer.append(sep).append(d[i]);
            } else {
                buffer.append(d[i]);
            }
        }
        return buffer.toString();
    }

    /**
     * Returns a string with the specified number of whitespace.
     *
     * @param n the number of whitespace characters
     * @return a string with the specified number of blanks
     */
    public static String whitespace(int n) {
        if (n < WHITESPACE_BUFFER_LENGTH) {
            return WHITESPACE_BUFFER.substring(0, n);
        }
        char[] buf = new char[n];
        for (int i = 0; i < n; i++) {
            buf[i] = WHITESPACE_BUFFER.charAt(0);
        }
        return new String(buf);
    }

    /**
     * Returns a string with the specified number of whitespace.
     *
     * @param n the number of whitespace characters
     * @return a string with the specified number of blanks
     */
    public static StringBuilder whitespace(StringBuilder buf, int n) {
        while (n >= WHITESPACE_BUFFER_LENGTH) {
            buf.append(WHITESPACE_BUFFER);
            n -= WHITESPACE_BUFFER_LENGTH;
        }
        return n > 0 ? buf.append(WHITESPACE_BUFFER, 0, n) : buf;
    }
}

Related

  1. formattedFromDouble(double d, int scale, Locale locale)
  2. formattedFromLong(long l, Locale locale)
  3. formattedToDouble(String str, Locale loc)
  4. formatTime(Calendar calendar, String format)
  5. formatTime(final double theTime)
  6. formatToString(double num, int decPlaces)
  7. formatValues(double x, double y)
  8. formatZahl(Number nZahl, int iNachkommastellen, Locale locale)