Java Code Examples for java.text.DecimalFormat#setCurrency()

The following examples show how to use java.text.DecimalFormat#setCurrency() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
protected NumberFormat getNumberFormat(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
	format.setParseBigDecimal(true);
	format.setMaximumFractionDigits(this.fractionDigits);
	format.setMinimumFractionDigits(this.fractionDigits);
	if (this.roundingMode != null) {
		format.setRoundingMode(this.roundingMode);
	}
	if (this.currency != null) {
		format.setCurrency(this.currency);
	}
	if (this.pattern != null) {
		format.applyPattern(this.pattern);
	}
	return format;
}
 
Example 2
@Override
protected NumberFormat getNumberFormat(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
	format.setParseBigDecimal(true);
	format.setMaximumFractionDigits(this.fractionDigits);
	format.setMinimumFractionDigits(this.fractionDigits);
	if (this.roundingMode != null) {
		format.setRoundingMode(this.roundingMode);
	}
	if (this.currency != null) {
		format.setCurrency(this.currency);
	}
	if (this.pattern != null) {
		format.applyPattern(this.pattern);
	}
	return format;
}
 
Example 3
@Override
protected NumberFormat getNumberFormat(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
	format.setParseBigDecimal(true);
	format.setMaximumFractionDigits(this.fractionDigits);
	format.setMinimumFractionDigits(this.fractionDigits);
	if (this.roundingMode != null) {
		format.setRoundingMode(this.roundingMode);
	}
	if (this.currency != null) {
		format.setCurrency(this.currency);
	}
	if (this.pattern != null) {
		format.applyPattern(this.pattern);
	}
	return format;
}
 
Example 4
@Override
protected NumberFormat getNumberFormat(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
	format.setParseBigDecimal(true);
	format.setMaximumFractionDigits(this.fractionDigits);
	format.setMinimumFractionDigits(this.fractionDigits);
	if (this.roundingMode != null) {
		format.setRoundingMode(this.roundingMode);
	}
	if (this.currency != null) {
		format.setCurrency(this.currency);
	}
	if (this.pattern != null) {
		format.applyPattern(this.pattern);
	}
	return format;
}
 
Example 5
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 6 votes vote down vote up
public void test_setCurrency() {
    Locale locale = Locale.CANADA;
    DecimalFormat df = ((DecimalFormat) NumberFormat.getCurrencyInstance(locale));

    try {
        df.setCurrency(null);
        fail("Expected NullPointerException");
    } catch (NullPointerException e) {
    }

    Currency currency = Currency.getInstance("AED");
    df.setCurrency(currency);
    assertTrue("Returned incorrect currency", currency == df.getCurrency());
    assertEquals("Returned incorrect currency symbol", currency.getSymbol(locale),
            df.getDecimalFormatSymbols().getCurrencySymbol());
    assertEquals("Returned incorrect international currency symbol", currency.getCurrencyCode(),
            df.getDecimalFormatSymbols().getInternationalCurrencySymbol());
}
 
Example 6
Source Project: phonegapbootcampsite   File: Globalization.java    License: MIT License 6 votes vote down vote up
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);
    }
}
 
Example 7
Source Project: phonegapbootcampsite   File: Globalization.java    License: MIT License 6 votes vote down vote up
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);
    }
}
 
Example 8
Source Project: phonegapbootcampsite   File: Globalization.java    License: MIT License 6 votes vote down vote up
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);
    }
}
 
Example 9
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);
    }
}
 
Example 10
Source Project: jpHolo   File: Globalization.java    License: MIT License 6 votes vote down vote up
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);
    }
}
 
Example 11
static NumberFormat create(Locale locale, Currency currency) {
  // The order of these lines matter! For instance, swapping lines 3 and 4 causes 2 decimal places to always be shown.
  DecimalFormat formatter = new DecimalFormat();
  formatter.setCurrency(currency);
  formatter.applyLocalizedPattern(getLocalizedPattern(locale, currency));
  formatter.setMaximumFractionDigits(currency.getDefaultFractionDigits());
  formatter.setDecimalFormatSymbols(getDecimalFormatSymbols(locale, currency));
  return formatter;
}
 
Example 12
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_equals() {
    DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    DecimalFormat cloned = (DecimalFormat) format.clone();
    cloned.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    assertEquals(format, cloned);

    Currency c = Currency.getInstance(Locale.US);
    cloned.setCurrency(c);

    assertEquals(format, cloned);
}
 
Example 13
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testBug9087737() throws Exception {
    DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(Locale.US);
    // These shouldn't make valgrind unhappy.
    df.setCurrency(Currency.getInstance("CHF"));
    df.setCurrency(Currency.getInstance("GBP"));
}