下面列出了java.text.DecimalFormat#getCurrencyInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
/**
* @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();
}
/***
* 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();
}
}
}
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;
}