Example usage for java.text DecimalFormat toLocalizedPattern

List of usage examples for java.text DecimalFormat toLocalizedPattern

Introduction

In this page you can find the example usage for java.text DecimalFormat toLocalizedPattern.

Prototype

public String toLocalizedPattern() 

Source Link

Document

Synthesizes a localized pattern string that represents the current state of this Format object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();

    System.out.println(format.toLocalizedPattern());

}

From source file:com.feilong.core.text.NumberFormatUtil.java

/**
 *  {@link Number}  {@link RoundingMode} <code>numberPattern</code>?.
 *
 * @param value/*from ww w.  j  av  a  2s . com*/
 *            the value
 * @param numberPattern
 *            the pattern {@link NumberPattern}
 * @param roundingMode
 *            ?{@link RoundingMode}
 * @return  <code>value</code> null, {@link NullPointerException}<br>
 *          <code>numberPattern</code> null, {@link NullPointerException}<br>
 *          <code>numberPattern</code> blank, {@link IllegalArgumentException}<br>
 * @see DecimalFormat
 * @see <a href="../util/NumberUtil.html#RoundingMode">JAVA 8??</a>
 */
public static String format(Number value, String numberPattern, RoundingMode roundingMode) {
    Validate.notNull(value, "value can't be null!");
    Validate.notBlank(numberPattern, "numberPattern can't be null!");

    // applyPattern(pattern, false)
    DecimalFormat decimalFormat = new DecimalFormat(numberPattern);

    // ? RoundingMode.HALF_EVEN  ?,?.?,?.??,?,??.???1?,???.
    // 1.15>1.2 1.25>1.2
    if (null != roundingMode) {
        decimalFormat.setRoundingMode(roundingMode);
    }
    String result = decimalFormat.format(value);
    LOGGER.trace("input:[{}],with:[{}]=[{}],localizedPattern:[{}]", value, numberPattern, result,
            decimalFormat.toLocalizedPattern());
    return result;
}

From source file:com.sunchenbin.store.feilong.core.text.NumberFormatUtil.java

/**
 *  {@link Number}  {@link RoundingMode} numberPattern?.
 *
 * @param value//from  www.j  a  v  a  2  s.  com
 *            the value
 * @param numberPattern
 *            the pattern {@link NumberPattern}
 * @param roundingMode
 *            ?{@link RoundingMode}
 * @return  null
 * @see NumberPattern
 * @see DecimalFormat
 * @see <a href="../util/NumberUtil.html#RoundingMode">JAVA 8??</a>
 */
public static String format(Number value, String numberPattern, RoundingMode roundingMode) {
    if (null == value) {
        throw new NullPointerException("the value is null or empty!");
    }

    if (null == numberPattern) {
        throw new NullPointerException("the numberPattern is null or empty!");
    }
    try {
        // applyPattern(pattern, false)
        DecimalFormat decimalFormat = new DecimalFormat(numberPattern);

        // ? RoundingMode.HALF_EVEN
        // ?,?.?,?.??,?,??.???1?,???.
        // 1.15>1.2 1.25>1.2
        if (null != roundingMode) {
            decimalFormat.setRoundingMode(roundingMode);
        }
        String format = decimalFormat.format(value);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("value:[{}], pattern:[{}],return:[{}],decimalFormat.toLocalizedPattern():[{}]", value,
                    numberPattern, format, decimalFormat.toLocalizedPattern()//?? Format ????. 
            );
        }
        return format;
    } catch (Exception e) {
        Object[] objects = { e.getMessage(), value, numberPattern };
        LOGGER.error("{},value:[{}],pattern:[{}]", objects);
        LOGGER.error(e.getClass().getName(), e);
    }
    return StringUtils.EMPTY;
}

From source file:org.pentaho.di.core.util.StringEvaluator.java

private void populateConversionMetaList() {

    int[] trimTypes;
    if (tryTrimming) {
        trimTypes = new int[] { ValueMetaInterface.TRIM_TYPE_NONE, ValueMetaInterface.TRIM_TYPE_BOTH, };
    } else {//  ww w  .  j a v  a2s . com
        trimTypes = new int[] { ValueMetaInterface.TRIM_TYPE_NONE, };
    }

    for (int trimType : trimTypes) {
        for (String format : getDateFormats()) {
            ValueMetaInterface conversionMeta = new ValueMetaDate("date");
            conversionMeta.setConversionMask(format);
            conversionMeta.setTrimType(trimType);
            conversionMeta.setDateFormatLenient(false);
            evaluationResults.add(new StringEvaluationResult(conversionMeta));
        }

        EvalResultBuilder numberUsBuilder = new EvalResultBuilder("number-us", ValueMetaInterface.TYPE_NUMBER,
                15, trimType, ".", ",");
        EvalResultBuilder numberEuBuilder = new EvalResultBuilder("number-eu", ValueMetaInterface.TYPE_NUMBER,
                15, trimType, ",", ".");

        for (String format : getNumberFormats()) {

            if (format.equals("#") || format.equals("0")) {
                // skip the integer ones. we'll get those later
                continue;
            }

            int precision = determinePrecision(format);
            evaluationResults.add(numberUsBuilder.format(format, precision).build());
            evaluationResults.add(numberEuBuilder.format(format, precision).build());
        }

        // Try the locale's Currency
        DecimalFormat currencyFormat = ((DecimalFormat) NumberFormat.getCurrencyInstance());

        ValueMetaInterface conversionMeta = new ValueMetaNumber("number-currency");
        // replace the universal currency symbol with the locale's currency symbol for user recognition
        String currencyMask = currencyFormat.toLocalizedPattern().replace("\u00A4",
                currencyFormat.getCurrency().getSymbol());
        conversionMeta.setConversionMask(currencyMask);
        conversionMeta.setTrimType(trimType);
        conversionMeta.setDecimalSymbol(
                String.valueOf(currencyFormat.getDecimalFormatSymbols().getDecimalSeparator()));
        conversionMeta.setGroupingSymbol(
                String.valueOf(currencyFormat.getDecimalFormatSymbols().getGroupingSeparator()));
        conversionMeta.setCurrencySymbol(currencyFormat.getCurrency().getSymbol());
        conversionMeta.setLength(15);
        int currencyPrecision = currencyFormat.getCurrency().getDefaultFractionDigits();
        conversionMeta.setPrecision(currencyPrecision);

        evaluationResults.add(new StringEvaluationResult(conversionMeta));

        // add same mask w/o currency symbol
        String currencyMaskAsNumeric = currencyMask
                .replaceAll(Pattern.quote(currencyFormat.getCurrency().getSymbol()), "");
        evaluationResults.add(numberUsBuilder.format(currencyMaskAsNumeric, currencyPrecision).build());
        evaluationResults.add(numberEuBuilder.format(currencyMaskAsNumeric, currencyPrecision).build());

        // Integer
        //
        conversionMeta = new ValueMetaInteger("integer");
        conversionMeta.setConversionMask("#");
        conversionMeta.setLength(15);
        evaluationResults.add(new StringEvaluationResult(conversionMeta));

        conversionMeta = new ValueMetaInteger("integer");
        conversionMeta.setConversionMask(" #");
        conversionMeta.setLength(15);
        evaluationResults.add(new StringEvaluationResult(conversionMeta));

        // Add support for left zero padded integers
        //
        for (int i = 1; i <= 15; i++) {

            String mask = " ";
            for (int x = 0; x < i; x++) {
                mask += "0";
            }
            mask += ";-";
            for (int x = 0; x < i; x++) {
                mask += "0";
            }

            conversionMeta = new ValueMetaInteger("integer-zero-padded-" + i);
            conversionMeta.setConversionMask(mask);
            conversionMeta.setLength(i);
            evaluationResults.add(new StringEvaluationResult(conversionMeta));
        }

        // Boolean
        //
        conversionMeta = new ValueMetaBoolean("boolean");
        evaluationResults.add(new StringEvaluationResult(conversionMeta));
    }
}