Search in sources :

Example 6 with Currency

use of java.util.Currency in project jpHolo by teusink.

the class Globalization method getCurrencyPattern.

/*
     * @Description: Returns a pattern string for formatting and parsing currency values according to the client's
     * user preferences and ISO 4217 currency code.
     * @Return: JSONObject
     *          Object.pattern {String}: The currency pattern for formatting and parsing currency values.
     *                                  The patterns follow Unicode Technical Standard #35
     *                                  http://unicode.org/reports/tr35/tr35-4.html
     *          Object.code {String}: The ISO 4217 currency code for the pattern.
     *          Object.fraction {Number}: The number of fractional digits to use when parsing and
     *                                  formatting currency.
     *          Object.rounding {Number}: The rounding increment to use when parsing and formatting.
     *          Object.decimal: {String}: The decimal symbol to use for parsing and formatting.
     *          Object.grouping: {String}: The grouping symbol to use for parsing and formatting.
     *
     * @throws: GlobalizationError.FORMATTING_ERROR
    */
private JSONObject getCurrencyPattern(JSONArray options) throws GlobalizationError {
    JSONObject obj = new JSONObject();
    try {
        //get ISO 4217 currency code
        String code = options.getJSONObject(0).getString(CURRENCYCODE);
        //uses java.text.DecimalFormat to format value
        DecimalFormat fmt = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.getDefault());
        //set currency format
        Currency currency = Currency.getInstance(code);
        fmt.setCurrency(currency);
        //return properties
        obj.put("pattern", fmt.toPattern());
        obj.put("code", currency.getCurrencyCode());
        obj.put("fraction", fmt.getMinimumFractionDigits());
        obj.put("rounding", Integer.valueOf(0));
        obj.put("decimal", String.valueOf(fmt.getDecimalFormatSymbols().getDecimalSeparator()));
        obj.put("grouping", String.valueOf(fmt.getDecimalFormatSymbols().getGroupingSeparator()));
        return obj;
    } catch (Exception ge) {
        throw new GlobalizationError(GlobalizationError.FORMATTING_ERROR);
    }
}
Also used : JSONObject(org.json.JSONObject) DecimalFormat(java.text.DecimalFormat) Currency(java.util.Currency) JSONException(org.json.JSONException)

Example 7 with Currency

use of java.util.Currency in project japid42 by branaway.

the class WebUtils method formatCurrency.

//    public static String formatCurrency(Number number, String currencyCode) {
//        Currency currency = Currency.getInstance(currencyCode);
//        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(new Locale(Lang.get()));
//        numberFormat.setCurrency(currency);
//        numberFormat.setMaximumFractionDigits(currency.getDefaultFractionDigits());
//        String s = numberFormat.format(number);
//        s = s.replace(currencyCode, I18N.getCurrencySymbol(currencyCode));
//        return s;
//    }
public static String formatCurrency(Number number, Locale locale) {
    Currency currency = Currency.getInstance(locale);
    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
    numberFormat.setCurrency(currency);
    numberFormat.setMaximumFractionDigits(currency.getDefaultFractionDigits());
    String s = numberFormat.format(number);
    s = s.replace(currency.getCurrencyCode(), currency.getSymbol(locale));
    return s;
}
Also used : Currency(java.util.Currency) NumberFormat(java.text.NumberFormat)

Example 8 with Currency

use of java.util.Currency in project j2objc by google.

the class NumberFormatTest method test_setCurrency.

// Test the currency symbol is correctly taken from ICU. Verifies that the fractional digits
// are not updated because DecimalFormat.setCurrency agrees not to change it.
public void test_setCurrency() throws Exception {
    NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
    // The Armenian Dram is a special case where the fractional digits are 0.
    Currency amd = Currency.getInstance("AMD");
    assertEquals(0, amd.getDefaultFractionDigits());
    // Armenian Dram ISO 4217 code.
    nf.setCurrency(amd);
    // Check DecimalFormat has not taken the
    assertEquals(2, nf.getMinimumFractionDigits());
    // currency specific fractional digits.
    assertEquals(2, nf.getMaximumFractionDigits());
    assertEquals("AMD50.00", nf.format(50.00));
    // Try and explicitly request fractional digits for the specified currency.
    nf.setMaximumFractionDigits(amd.getDefaultFractionDigits());
    assertEquals("AMD50", nf.format(50.00));
    nf = NumberFormat.getCurrencyInstance(Locale.US);
    // Euro sign.
    nf.setCurrency(Currency.getInstance("EUR"));
    assertEquals("€50.00", nf.format(50.00));
    // Japanese Yen symbol.
    nf.setCurrency(Currency.getInstance("JPY"));
    assertEquals("¥50.00", nf.format(50.00));
    // Swiss Franc ISO 4217 code.
    nf.setCurrency(Currency.getInstance("CHF"));
    assertEquals("CHF50.00", nf.format(50.00));
}
Also used : Currency(java.util.Currency) NumberFormat(java.text.NumberFormat)

Example 9 with Currency

use of java.util.Currency in project j2objc by google.

the class NumberFormatTest method test_setCurrencyLjava_util_Currency.

/**
     * @tests java.text.NumberFormat#setCurrency(java.util.Currency)
     */
public void test_setCurrencyLjava_util_Currency() {
    // Test for method void setCurrency(java.util.Currency)
    // a subclass that supports currency formatting
    Currency currA = Currency.getInstance("ARS");
    NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU"));
    format.setCurrency(currA);
    assertSame("Returned incorrect currency", currA, format.getCurrency());
    // a subclass that doesn't support currency formatting
    ChoiceFormat cformat = new ChoiceFormat("0#Less than one|1#one|1<Between one and two|2<Greater than two");
    try {
        ((NumberFormat) cformat).setCurrency(currA);
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    }
}
Also used : Locale(java.util.Locale) Currency(java.util.Currency) ChoiceFormat(java.text.ChoiceFormat) NumberFormat(java.text.NumberFormat)

Example 10 with Currency

use of java.util.Currency in project j2objc by google.

the class NumberFormat method adjustForCurrencyDefaultFractionDigits.

// J2ObjC: This method exists in DecimalFormat.java in android source.
// It exists in sun.util.locale.provider.NumberFormatProviderImpl in openjdk source.
/**
     * Adjusts the minimum and maximum fraction digits to values that
     * are reasonable for the currency's default fraction digits.
     */
private static void adjustForCurrencyDefaultFractionDigits(DecimalFormat format, DecimalFormatSymbols symbols) {
    Currency currency = symbols.getCurrency();
    if (currency == null) {
        try {
            currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
        } catch (IllegalArgumentException e) {
        }
    }
    if (currency != null) {
        int digits = currency.getDefaultFractionDigits();
        if (digits != -1) {
            int oldMinDigits = format.getMinimumFractionDigits();
            // Try to adjust all of them in a reasonable way.
            if (oldMinDigits == format.getMaximumFractionDigits()) {
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            } else {
                format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
                format.setMaximumFractionDigits(digits);
            }
        }
    }
}
Also used : Currency(java.util.Currency)

Aggregations

Currency (java.util.Currency)58 Locale (java.util.Locale)21 BigDecimal (java.math.BigDecimal)8 DecimalFormat (java.text.DecimalFormat)7 DecimalFormatSymbols (java.text.DecimalFormatSymbols)7 NumberFormat (java.text.NumberFormat)6 ChoiceFormat (java.text.ChoiceFormat)3 HashSet (java.util.HashSet)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 ArrayList (java.util.ArrayList)2 Exchange (org.apache.camel.Exchange)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1