Search in sources :

Example 46 with Currency

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

the class NumberFormatTest method test_getCurrency.

/**
     * @tests java.text.NumberFormat#getCurrency()
     */
public void test_getCurrency() {
    // Test for method java.util.Currency getCurrency()
    // a subclass that supports currency formatting
    Currency currH = Currency.getInstance("HUF");
    NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU"));
    assertSame("Returned incorrect currency", currH, 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).getCurrency();
        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 47 with Currency

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

the class DecimalFormatSymbolsTest method testSetNulInternationalCurrencySymbol.

//    // https://code.google.com/p/android/issues/detail?id=79925
//    public void testSetSameCurrency() throws Exception {
//        DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
//        dfs.setCurrency(Currency.getInstance("USD"));
//        assertEquals("$", dfs.getCurrencySymbol());
//        dfs.setCurrencySymbol("poop");
//        assertEquals("poop", dfs.getCurrencySymbol());
//        dfs.setCurrency(Currency.getInstance("USD"));
//        assertEquals("$", dfs.getCurrencySymbol());
//    }
public void testSetNulInternationalCurrencySymbol() throws Exception {
    Currency usd = Currency.getInstance("USD");
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
    dfs.setCurrency(usd);
    assertEquals(usd, dfs.getCurrency());
    assertEquals("$", dfs.getCurrencySymbol());
    assertEquals("USD", dfs.getInternationalCurrencySymbol());
    // Setting the international currency symbol to null sets the currency to null too,
    // but not the currency symbol.
    dfs.setInternationalCurrencySymbol(null);
    assertEquals(null, dfs.getCurrency());
    assertEquals("$", dfs.getCurrencySymbol());
    assertEquals(null, dfs.getInternationalCurrencySymbol());
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) Currency(java.util.Currency)

Example 48 with Currency

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

the class DecimalFormatTest method testSetCurrency_symbolOrigin.

// Confirm the currency symbol used by a format is determined by the locale of the format
// not the current default Locale.
public void testSetCurrency_symbolOrigin() {
    Currency currency = Currency.getInstance("CNY");
    Locale locale1 = Locale.CHINA;
    Locale locale2 = Locale.US;
    String locale1Symbol = currency.getSymbol(locale1);
    String locale2Symbol = currency.getSymbol(locale2);
    // This test only works if we can tell where the symbol came from, which requires they are
    // different across the two locales chosen.
    assertFalse(locale1Symbol.equals(locale2Symbol));
    Locale originalLocale = Locale.getDefault();
    try {
        Locale.setDefault(locale1);
        String amountDefaultLocale1 = formatArbitraryCurrencyAmountInLocale(currency, locale2);
        Locale.setDefault(locale2);
        String amountDefaultLocale2 = formatArbitraryCurrencyAmountInLocale(currency, locale2);
        // This used to fail because Currency.getSymbol() was used without providing the
        // format's locale.
        assertEquals(amountDefaultLocale1, amountDefaultLocale2);
    } finally {
        Locale.setDefault(originalLocale);
    }
}
Also used : Locale(java.util.Locale) Currency(java.util.Currency)

Example 49 with Currency

use of java.util.Currency in project cordova-android-chromeview by thedracle.

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", new Integer(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 50 with Currency

use of java.util.Currency in project Botnak by Gocnak.

the class DonationManager method checkDonations.

public void checkDonations(boolean single) {
    int limit = (single ? 5 : 100);
    String url = "https://streamtip.com/api/tips?client_id=" + getClientID() + "&access_token=" + getAccessCode() + "&limit=" + limit;
    try {
        String line = Utils.createAndParseBufferedReader(new URL(url).openStream());
        if (!line.isEmpty()) {
            JSONObject outerShell = new JSONObject(line);
            int status = outerShell.getInt("status");
            int count = outerShell.getInt("_count");
            if (status == 200) {
                //ensure there's no problem with the site
                JSONArray tipsArray = outerShell.getJSONArray("tips");
                for (int i = (single ? tipsArray.length() - 1 : count - 1); i > -1; i--) {
                    JSONObject tip = tipsArray.getJSONObject(i);
                    try {
                        Currency c = Currency.getInstance(tip.getString("currencyCode"));
                        getCurrencyFormat().setCurrency(c);
                    } catch (Exception e) {
                        GUIMain.log("Unknown currency code: " + tip.getString("currencyCode"));
                        getCurrencyFormat().setCurrency(Currency.getInstance("USD"));
                    }
                    if (lastDonation != null) {
                        if (lastDonation.getDonationID().equals(tip.getString("_id"))) {
                            continue;
                        }
                    }
                    addDonation(tip, false);
                }
            }
        }
    } catch (Exception e) {
        GUIMain.log(e);
    }
}
Also used : JSONObject(lib.JSON.JSONObject) Currency(java.util.Currency) JSONArray(lib.JSON.JSONArray) URL(java.net.URL)

Aggregations

Currency (java.util.Currency)60 Locale (java.util.Locale)21 BigDecimal (java.math.BigDecimal)8 NumberFormat (java.text.NumberFormat)8 DecimalFormat (java.text.DecimalFormat)7 DecimalFormatSymbols (java.text.DecimalFormatSymbols)7 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 ParsePosition (java.text.ParsePosition)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