简体   繁体   中英

need to format currency for TextView

i parse a json object and suppose i have this value 1515.2777777777778 i need to parse the currency in this way:
€ 1'515,27

Is there a special class that can directly do the conversion? or should i do it in this way:

Double number = Double.valueOf(obj.getString("price"));
DecimalFormat decimalFormat = new DecimalFormat("€ #\\'###.00");
String prezzo = decimalFormat.format(number);

but even in this way it doesnt lieke the single apostrophe.

您可以使用NumberFormat.getCurrencyInstance()NumberFormat.getCurrencyInstance(Locale locale)来获取可用于格式化currentnmaty值的NumberFormat实例。

The format you need is € #,###.00 , , means you use a grouping separator.

Then, you need a DecimalFormatSymbols to specify the grouping symbol and the decimal symbol:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator('\'');
symbols.setDecimalSeparator(',');

DecimalFormat decimalFormat = new DecimalFormat("€ #,###.00", symbols);
String prezzo = decimalFormat.format(number);

Result € 1'515,28

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM