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

The following examples show how to use java.text.DecimalFormat#setCurrency() . 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
Source File: CurrencyStyleFormatter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: CurrencyStyleFormatter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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
Source File: CurrencyStyleFormatter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: CurrencyStyleFormatter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 File: DecimalFormatTest.java    From j2objc with 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 File: Globalization.java    From phonegapbootcampsite with 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 File: Globalization.java    From phonegapbootcampsite with 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 File: Globalization.java    From phonegapbootcampsite with 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
Source File: Globalization.java    From cordova-android-chromeview with Apache License 2.0 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", 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 File: Globalization.java    From jpHolo with 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
Source File: LegacyMoneyFormatFactory.java    From template-compiler with Apache License 2.0 5 votes vote down vote up
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 File: DecimalFormatTest.java    From j2objc with 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 File: DecimalFormatTest.java    From j2objc with 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"));
}