Example usage for java.text DecimalFormat getCurrency

List of usage examples for java.text DecimalFormat getCurrency

Introduction

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

Prototype

@Override
public Currency getCurrency() 

Source Link

Document

Gets the currency used by this decimal format when formatting currency values.

Usage

From source file:Main.java

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

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

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();
    format.setCurrency(Currency.getInstance(Locale.GERMANY));
    System.out.println(format.getCurrency());

}

From source file:Main.java

@NonNull
static String convertAmount(int amount) {
    DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance();
    formatter.setCurrency(Currency.getInstance("EUR"));
    String symbol = formatter.getCurrency().getSymbol();
    formatter.setNegativePrefix(symbol + "-");
    formatter.setNegativeSuffix("");

    return formatter.format((double) amount / 100.0);
}

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 {/*from   w ww  .j a  v a  2 s. co  m*/
        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));
    }
}