Java Locale Format format(String message, Object[] params)

Here you can find the source of format(String message, Object[] params)

Description

Apply printf() like formatting to a string.

License

Apache License

Parameter

Parameter Description
message the string with embedded formatting info eg. "This is a test %2.2"
params array of values to format into the string

Return

The formatted string

Declaration

public static String format(String message, Object[] params) 

Method Source Code

//package com.java2s;
/* NOTICE: This file has been changed by Plutext Pty Ltd for use in docx4j.
 * The package name has been changed; there may also be other changes.
 * /*from   www.java 2 s  .c  om*/
 * This notice is included to meet the condition in clause 4(b) of the License. 
 */

import java.text.FieldPosition;
import java.text.NumberFormat;

import java.util.Locale;

public class Main {
    /**
     *  Apply printf() like formatting to a string.
     *  Primarily used for logging.
     * @param  message  the string with embedded formatting info
     *                 eg. "This is a test %2.2"
     * @param  params   array of values to format into the string
     * @return          The formatted string
     */
    public static String format(String message, Object[] params) {
        int currentParamNumber = 0;
        StringBuffer formattedMessage = new StringBuffer();
        for (int i = 0; i < message.length(); i++) {
            if (message.charAt(i) == '%') {
                if (currentParamNumber >= params.length) {
                    formattedMessage.append("?missing data?");
                } else if ((params[currentParamNumber] instanceof Number) && (i + 1 < message.length())) {
                    i += matchOptionalFormatting((Number) params[currentParamNumber++], message.substring(i + 1),
                            formattedMessage);
                } else {
                    formattedMessage.append(params[currentParamNumber++].toString());
                }
            } else {
                if ((message.charAt(i) == '\\') && (i + 1 < message.length()) && (message.charAt(i + 1) == '%')) {
                    formattedMessage.append('%');
                    i++;
                } else {
                    formattedMessage.append(message.charAt(i));
                }
            }
        }
        return formattedMessage.toString();
    }

    private static int matchOptionalFormatting(Number number, String formatting, StringBuffer outputTo) {
        NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
        if ((0 < formatting.length()) && Character.isDigit(formatting.charAt(0))) {
            numberFormat.setMinimumIntegerDigits(Integer.parseInt(formatting.charAt(0) + ""));
            if ((2 < formatting.length()) && (formatting.charAt(1) == '.')
                    && Character.isDigit(formatting.charAt(2))) {
                numberFormat.setMaximumFractionDigits(Integer.parseInt(formatting.charAt(2) + ""));
                numberFormat.format(number, outputTo, new FieldPosition(0));
                return 3;
            }
            numberFormat.format(number, outputTo, new FieldPosition(0));
            return 1;
        } else if ((0 < formatting.length()) && (formatting.charAt(0) == '.')) {
            if ((1 < formatting.length()) && Character.isDigit(formatting.charAt(1))) {
                numberFormat.setMaximumFractionDigits(Integer.parseInt(formatting.charAt(1) + ""));
                numberFormat.format(number, outputTo, new FieldPosition(0));
                return 2;
            }
        }
        numberFormat.format(number, outputTo, new FieldPosition(0));
        return 1;
    }
}

Related

  1. format(Number number)
  2. format(Number number, Locale locale)
  3. format(Object input, int style)
  4. format(String bundleName, Locale locale, String key, Object arg1)
  5. format(String format, Date date, Locale locale)
  6. format(String pattern, Date date)
  7. format12(final long ticks)
  8. formatAdena(long amount)
  9. formatAdena(long amount)