Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 i t doesnt lieke the single apostrophe.

like image 806
Pheonix7 Avatar asked Dec 13 '13 16:12

Pheonix7


2 Answers

You can use NumberFormat.getCurrencyInstance() or NumberFormat.getCurrencyInstance(Locale locale) to get the NumberFormat instance that can be used to format currencty values.

like image 141
Apoorv Avatar answered Oct 21 '22 03:10

Apoorv


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

like image 33
njzk2 Avatar answered Oct 21 '22 02:10

njzk2