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

The following examples show how to use java.text.DecimalFormat#getCurrencyInstance() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: DefaultMonetaryAmountFormat.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
private List<FormatToken> initPattern(String pattern, AmountFormatContext context) {
    Locale locale = context.get(Locale.class);
    DecimalFormat format = (DecimalFormat)DecimalFormat.getCurrencyInstance(locale);
    CurrencyStyle currencyStyle = context.get(CurrencyStyle.class);
    List<String> patternParts = tokenizePattern(pattern, format);
    List<FormatToken> tokens = new ArrayList<>(3);
    for(String p:patternParts){
        if (isNumberToken(p)) {
            tokens.add(new AmountNumberToken(context, p.substring(4)));
        } else if(isCurrencyToken(p)){
            tokens.add(new CurrencyToken(currencyStyle, context));
        } else{
            if(!p.isEmpty()) {
                tokens.add(new LiteralToken(p));
            }
        }
    }
    return tokens;
}
 
Example 7
Source File: NumberUtil.java    From sagacity-sqltoy with Apache License 2.0 5 votes vote down vote up
/**
 * @todo 格式化不同币种的金额
 * @param target
 * @param pattern
 * @param locale
 * @return
 */
public static String formatCurrency(Object target, String pattern, String locale) {
	if (target == null) {
		return null;
	}
	if (pattern == null) {
		return target.toString();
	}
	try {
		String tmpStr = target.toString().replace(",", "").trim().toLowerCase();
		if (tmpStr.equals("") || tmpStr.equals("null") || tmpStr.equals("nan")) {
			return "";
		}
		String lowPattern = pattern.toLowerCase();
		BigDecimal tmp = new BigDecimal(tmpStr);
		if (lowPattern.equals(Pattern.CAPITAL)) {
			return numberToChina(tmpStr, false);
		}
		if (lowPattern.equals(Pattern.CAPITAL_MONEY) || lowPattern.equals(Pattern.CAPITAL_RMB)) {
			return toCapitalMoney(tmp);
		}
		DecimalFormat df = (DecimalFormat) (StringUtil.isBlank(locale) ? DecimalFormat.getCurrencyInstance()
				: DecimalFormat.getCurrencyInstance(new Locale(locale)));
		df.applyPattern(pattern);
		return df.format(tmp);
	} catch (Exception e) {
		e.printStackTrace();
		logger.error(e.getMessage());
	}
	return target.toString();
}
 
Example 8
Source File: CurrencyEditText.java    From currency_edittext with Apache License 2.0 5 votes vote down vote up
/***
 * If user does not provide a valid locale it throws IllegalArgumentException.
 *
 * If throws an IllegalArgumentException the locale sets to default locale
 */
private void initSettings() {
    boolean success = false;
    while (!success) {
        try {
            if (fractionDigit == 0) {
                fractionDigit = Currency.getInstance(locale).getDefaultFractionDigits();
            }

            DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
            if (mGroupDivider > 0)
                symbols.setGroupingSeparator(mGroupDivider);
            groupDivider = symbols.getGroupingSeparator();

            if (mMonetaryDivider > 0)
                symbols.setMonetaryDecimalSeparator(mMonetaryDivider);
            monetaryDivider = symbols.getMonetaryDecimalSeparator();

            currencySymbol = symbols.getCurrencySymbol();

            DecimalFormat df = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale);
            numberFormat = new DecimalFormat(df.toPattern(), symbols);

            if (mDecimalPoints > 0) {
                numberFormat.setMinimumFractionDigits(mDecimalPoints);
            }

            success = true;
        } catch (IllegalArgumentException e) {
            Log.e(getClass().getCanonicalName(), e.getMessage());
            locale = getDefaultLocale();
        }
    }
}
 
Example 9
Source File: DefaultMonetaryAmountFormat.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
private String resolvePattern(AmountFormatContext amountFormatContext) {
    String pattern = amountFormatContext.getText(PATTERN);
    if (pattern == null) {
        DecimalFormat currencyDecimalFormat = (DecimalFormat) DecimalFormat.getCurrencyInstance(amountFormatContext.getLocale());
        pattern = MoneyUtils.replaceNbspWithSpace(currencyDecimalFormat.toPattern());
    }
    return pattern;
}