Search in sources :

Example 21 with Currency

use of java.util.Currency in project jdk8u_jdk by JetBrains.

the class CurrencyTest method checkCountryCurrency.

static void checkCountryCurrency(String countryCode, String expected) {
    Locale locale = new Locale("", countryCode);
    Currency currency = Currency.getInstance(locale);
    String code = (currency != null) ? currency.getCurrencyCode() : null;
    if (!(expected == null ? code == null : expected.equals(code))) {
        throw new RuntimeException("Wrong currency for " + locale.getDisplayCountry() + ": expected " + expected + ", got " + code);
    }
}
Also used : Locale(java.util.Locale) Currency(java.util.Currency)

Example 22 with Currency

use of java.util.Currency in project fastjson by alibaba.

the class CurrencyTest4 method test_0.

public void test_0() throws Exception {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("currency", "CNY");
    String text = JSON.toJSONString(jsonObject);
    Currency currency = JSON.parseObject(text, Currency.class);
    assertSame(Currency.getInstance("CNY"), currency);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) Currency(java.util.Currency)

Example 23 with Currency

use of java.util.Currency in project fastjson by alibaba.

the class CurrencyTest4 method test_1.

public void test_1() throws Exception {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("symbol", "CNY");
    String text = JSON.toJSONString(jsonObject);
    Currency currency = JSON.parseObject(text, Currency.class);
    assertSame(Currency.getInstance("CNY"), currency);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) Currency(java.util.Currency)

Example 24 with Currency

use of java.util.Currency in project adempiere by adempiere.

the class VSetup method dynInit.

//	jbInit
/**
	 *	Dynamic Init
	 */
private void dynInit() {
    // Using current locale for default values - teo_sarca [ 1691388 ] 
    Locale locale = Locale.getDefault();
    Currency currency = Currency.getInstance(locale);
    //	Currency
    // teo_sarca [ 1691388 ]
    String sql = "SELECT C_Currency_ID, Description, ISO_Code FROM C_Currency ORDER BY 2";
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = DB.createStatement();
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            fCurrency.addItem(new KeyNamePair(rs.getInt(1), rs.getString(2)));
            // Currency from locale will be the default currency - teo_sarca [ 1691388 ]
            if (currency != null && currency.getCurrencyCode().equals(rs.getString(3)))
                fCurrency.setSelectedIndex(fCurrency.getItemCount() - 1);
        }
    } catch (SQLException e1) {
        log.log(Level.SEVERE, "VSetup.dynInit -currency", e1);
    } finally {
        DB.close(rs, stmt);
        rs = null;
        stmt = null;
    }
    //	Country
    int C_Country_ID = 0;
    // teo_sarca [ 1691388 ]
    sql = "SELECT C_Country_ID, Name, CountryCode FROM C_Country ORDER BY 2";
    try {
        stmt = DB.createStatement();
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            fCountry.addItem(new KeyNamePair(rs.getInt(1), rs.getString(2)));
            // Country from locale will be the default country - teo_sarca [ 1691388 ]
            if (locale.getCountry().equals(rs.getString(3))) {
                fCountry.setSelectedIndex(fCountry.getItemCount() - 1);
                C_Country_ID = rs.getInt(1);
            }
        }
    } catch (SQLException e1) {
        log.log(Level.SEVERE, "VSetup.dynInit -country", e1);
    } finally {
        DB.close(rs, stmt);
        rs = null;
        stmt = null;
    }
    //	Region (optional)
    sql = "SELECT C_Region_ID, Name, C_Country_ID FROM C_Region ORDER BY C_Country_ID, Name";
    boolean isSelected = false;
    try {
        fRegion.addItem(new KeyNamePair(0, " "));
        stmt = DB.createStatement();
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            fRegion.addItem(new KeyNamePair(rs.getInt(1), rs.getString(2)));
            // First region for selected country will be the default - teo_sarca [ 1691388 ]
            if (!isSelected && rs.getInt(3) == C_Country_ID) {
                fRegion.setSelectedIndex(fRegion.getItemCount() - 1);
                isSelected = true;
            }
        }
    } catch (SQLException e1) {
        log.log(Level.SEVERE, "VSetup.dynInit -region", e1);
    } finally {
        DB.close(rs, stmt);
        rs = null;
        stmt = null;
    }
    //  General Listeners
    confirmPanel.addActionListener(this);
    buttonLoadAcct.addActionListener(this);
    confirmPanel.getOKButton().setEnabled(false);
}
Also used : Locale(java.util.Locale) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Currency(java.util.Currency) ResultSet(java.sql.ResultSet) KeyNamePair(org.compiere.util.KeyNamePair)

Example 25 with Currency

use of java.util.Currency in project lucene-solr by apache.

the class CurrencyValue method parse.

/**
   * Constructs a new currency value by parsing the specific input.
   * <p/>
   * Currency values are expected to be in the format &lt;amount&gt;,&lt;currency code&gt;,
   * for example, "500,USD" would represent 5 U.S. Dollars.
   * <p/>
   * If no currency code is specified, the default is assumed.
   *
   * @param externalVal The value to parse.
   * @param defaultCurrency The default currency.
   * @return The parsed CurrencyValue.
   */
public static CurrencyValue parse(String externalVal, String defaultCurrency) {
    if (externalVal == null) {
        return null;
    }
    String amount = externalVal;
    String code = defaultCurrency;
    if (externalVal.contains(",")) {
        String[] amountAndCode = externalVal.split(",");
        amount = amountAndCode[0];
        code = amountAndCode[1];
    }
    if (amount.equals("*")) {
        return null;
    }
    Currency currency = CurrencyField.getCurrency(code);
    if (currency == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Currency code not supported by this JVM: " + code);
    }
    try {
        double value = Double.parseDouble(amount);
        long currencyValue = Math.round(value * Math.pow(10.0, currency.getDefaultFractionDigits()));
        return new CurrencyValue(currencyValue, code);
    } catch (NumberFormatException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
}
Also used : Currency(java.util.Currency) SolrException(org.apache.solr.common.SolrException)

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