11package com .hyperwallet .android .ui .common .util ;
22
3- import static com .hyperwallet .android .model .transfer .Transfer .EMPTY_STRING ;
4-
53import android .content .Context ;
64
75import androidx .annotation .VisibleForTesting ;
1210
1311import java .io .IOException ;
1412import java .io .InputStream ;
13+ import java .math .RoundingMode ;
1514import java .text .DecimalFormat ;
1615import java .text .DecimalFormatSymbols ;
1716import java .text .NumberFormat ;
1817import java .util .ArrayList ;
1918import java .util .Currency ;
19+ import java .util .HashMap ;
2020import java .util .List ;
21+ import java .util .Locale ;
2122
2223public class CurrencyParser {
2324 private static CurrencyParser instance ;
2425 private static final String CURRENCY_LIST = "currency.json" ;
25- private List <CurrencyDetails > currencyList ;
26+ private final List <CurrencyDetails > currencyList ;
27+ private final HashMap <String , LocaleDetails > localeList = new HashMap <>();
2628 private static final String REGEX_REMOVE_EMPTY_SPACE = "^\\ s+|\\ s+$" ;
2729
2830
2931 private CurrencyParser (Context context ) {
3032 currencyList = populateCurrencyList (readJSONFromAsset (context ));
33+ setLocaleList ();
3134 }
3235
3336 public static CurrencyParser getInstance (Context context ) {
@@ -73,21 +76,6 @@ private List<CurrencyDetails> populateCurrencyList(String currencyList) {
7376 return mCurrencyDetailsList ;
7477 }
7578
76- /**
77- * Formats the currency as per currency code.
78- *
79- * @param currency Any currency symbol.
80- * @param amount Any valid number in decimal.
81- * @return Returns the formatted number as per currency.
82- */
83- public String formatCurrency (String currency , String amount ) {
84- int numberOfFractions = getNumberOfFractionDigits (currency );
85- NumberFormat format = NumberFormat .getCurrencyInstance ();
86- format .setMinimumFractionDigits (numberOfFractions );
87- format .setCurrency (Currency .getInstance (currency ));
88- return format .format (Double .parseDouble (amount ));
89- }
90-
9179 @ VisibleForTesting
9280 int getNumberOfFractionDigits (String currencyCode ) {
9381 for (CurrencyDetails list : currencyList ) {
@@ -108,15 +96,21 @@ int getNumberOfFractionDigits(String currencyCode) {
10896 */
10997
11098 public String formatCurrencyWithSymbol (String currency , String amount ) {
111- DecimalFormat currencyFormatter = (DecimalFormat ) DecimalFormat .getCurrencyInstance ();
99+ DecimalFormat currencyFormatter ;
100+ if (localeList .containsKey (currency )) {
101+ LocaleDetails locale = localeList .get (currency );
102+ currencyFormatter = (DecimalFormat ) DecimalFormat .getCurrencyInstance ((new Locale (locale .getLanguage (),locale .getCountryCode ())));
103+ }else {
104+ currencyFormatter = (DecimalFormat ) DecimalFormat .getCurrencyInstance ();
105+ }
112106 CurrencyDetails currencyDetails = getCurrency (currency );
113107 currencyFormatter .setMinimumFractionDigits (currencyDetails == null ? 0 : currencyDetails .getDecimals ());
114108 currencyFormatter .setCurrency (Currency .getInstance (currency ));
115109 DecimalFormatSymbols decimalFormatSymbols = currencyFormatter .getDecimalFormatSymbols ();
116110 decimalFormatSymbols .setCurrencySymbol ("" );
117111 currencyFormatter .setDecimalFormatSymbols (decimalFormatSymbols );
118- String formattedAmount = currencyFormatter .format (Double .parseDouble (amount )). replaceAll ( REGEX_REMOVE_EMPTY_SPACE , EMPTY_STRING ) ;
119- return currencyDetails == null ? "" : currencyDetails .getSymbol () + formattedAmount ;
112+ String formattedAmount = currencyFormatter .format (Double .parseDouble (amount ));
113+ return currencyDetails == null ? "" : currencyDetails .getSymbol () + formattedAmount . trim () ;
120114 }
121115
122116 public CurrencyDetails getCurrency (String currencyCode ) {
@@ -127,4 +121,122 @@ public CurrencyDetails getCurrency(String currencyCode) {
127121 }
128122 return null ;
129123 }
124+
125+ /**
126+ *
127+ */
128+ public void setLocaleList () {
129+ localeList .clear ();
130+ localeList .put ("AED" ,new LocaleDetails ("en" ,"AE" ));
131+ localeList .put ("ALL" ,new LocaleDetails ("en" ,"US" ));
132+ localeList .put ("AMD" ,new LocaleDetails ("hy" ,"AM" ));
133+ localeList .put ("ARS" ,new LocaleDetails ("es" ,"AR" ));
134+ localeList .put ("AUD" ,new LocaleDetails ("en" ,"AU" ));
135+ localeList .put ("BAM" ,new LocaleDetails ("hr" ,"BA" ));
136+ localeList .put ("BDT" ,new LocaleDetails ("en" ,"BD" ));
137+ localeList .put ("BGN" ,new LocaleDetails ("bg" ,"BG" ));
138+ localeList .put ("BHD" ,new LocaleDetails ("en" ,"US" ));
139+ localeList .put ("BOB" ,new LocaleDetails ("qu" ,"BO" ));
140+ localeList .put ("BRL" ,new LocaleDetails ("en" ,"BR" ));
141+ localeList .put ("BWP" ,new LocaleDetails ("en" ,"BW" ));
142+ localeList .put ("CAD" ,new LocaleDetails ("en" ,"CA" ));
143+ localeList .put ("CHF" ,new LocaleDetails ("en" ,"CH" ));
144+ localeList .put ("CLP" ,new LocaleDetails ("es" ,"CL" ));
145+ localeList .put ("CNH" ,new LocaleDetails ("en" ,"CN" ));
146+ localeList .put ("CNY" ,new LocaleDetails ("en" ,"CN" ));
147+ localeList .put ("COP" ,new LocaleDetails ("es" ,"CO" ));
148+ localeList .put ("CZK" ,new LocaleDetails ("cs" ,"CZ" ));
149+ localeList .put ("DKK" ,new LocaleDetails ("en" ,"DK" ));
150+ localeList .put ("EEK" ,new LocaleDetails ("en" ,"US" ));
151+ localeList .put ("EGP" ,new LocaleDetails ("en" ,"US" ));
152+ localeList .put ("ETB" ,new LocaleDetails ("so" ,"ET" ));
153+ localeList .put ("EUR" ,new LocaleDetails ("es" ,"EA" ));
154+ localeList .put ("FJD" ,new LocaleDetails ("en" ,"FJ" ));
155+ localeList .put ("GBP" ,new LocaleDetails ("kw" ,"GB" ));
156+ localeList .put ("GHS" ,new LocaleDetails ("ee" ,"GH" ));
157+ localeList .put ("GMD" ,new LocaleDetails ("en" ,"GM" ));
158+ localeList .put ("HKD" ,new LocaleDetails ("en" ,"HK" ));
159+ localeList .put ("HRK" ,new LocaleDetails ("es" ,"HR" ));
160+ localeList .put ("HUF" ,new LocaleDetails ("hu" ,"HU" ));
161+ localeList .put ("IDR" ,new LocaleDetails ("jv" ,"ID" ));
162+ localeList .put ("ILS" ,new LocaleDetails ("he" ,"IL" ));
163+ localeList .put ("INR" ,new LocaleDetails ("en" ,"IN" ));
164+ localeList .put ("ISK" ,new LocaleDetails ("en" ,"US" ));
165+ localeList .put ("JMD" ,new LocaleDetails ("en" ,"JM" ));
166+ localeList .put ("JOD" ,new LocaleDetails ("en" ,"us" ));
167+ localeList .put ("JPY" ,new LocaleDetails ("en" ,"JP" ));
168+ localeList .put ("KES" ,new LocaleDetails ("guz" ,"KE" ));
169+ localeList .put ("KHR" ,new LocaleDetails ("km" ,"KH" ));
170+ localeList .put ("KRW" ,new LocaleDetails ("en" ,"KR" ));
171+ localeList .put ("KWD" ,new LocaleDetails ("en" ,"US" ));
172+ localeList .put ("KZT" ,new LocaleDetails ("ru" ,"KZ" ));
173+ localeList .put ("LAK" ,new LocaleDetails ("lo" ,"LA" ));
174+ localeList .put ("LKR" ,new LocaleDetails ("ta" ,"LK" ));
175+ localeList .put ("LSL" ,new LocaleDetails ("en" ,"US" ));
176+ localeList .put ("MAD" ,new LocaleDetails ("zgh" ,"MA" ));
177+ localeList .put ("MGA" ,new LocaleDetails ("en" ,"MG" ));
178+ localeList .put ("MRU" ,new LocaleDetails ("ff" ,"MR" ));
179+ localeList .put ("MUR" ,new LocaleDetails ("en" ,"MU" ));
180+ localeList .put ("MWK" ,new LocaleDetails ("en" ,"MW" ));
181+ localeList .put ("MXN" ,new LocaleDetails ("en" ,"MX" ));
182+ localeList .put ("MYR" ,new LocaleDetails ("en" ,"MY" ));
183+ localeList .put ("MZN" ,new LocaleDetails ("mgh" ,"MZ" ));
184+ localeList .put ("NAD" ,new LocaleDetails ("af" ,"NA" ));
185+ localeList .put ("NGN" ,new LocaleDetails ("en" ,"NG" ));
186+ localeList .put ("NOK" ,new LocaleDetails ("nn" ,"NO" ));
187+ localeList .put ("NPR" ,new LocaleDetails ("en" ,"US" ));
188+ localeList .put ("NZD" ,new LocaleDetails ("en" ,"PN" ));
189+ localeList .put ("OMR" ,new LocaleDetails ("ae" ,"OM" ));
190+ localeList .put ("PEN" ,new LocaleDetails ("en" ,"PE" ));
191+ localeList .put ("PGK" ,new LocaleDetails ("en" ,"PG" ));
192+ localeList .put ("PHP" ,new LocaleDetails ("ceb" ,"PH" ));
193+ localeList .put ("PKR" ,new LocaleDetails ("en" ,"PK" ));
194+ localeList .put ("PLN" ,new LocaleDetails ("pl" ,"PL" ));
195+ localeList .put ("QAR" ,new LocaleDetails ("en" ,"US" ));
196+ localeList .put ("RON" ,new LocaleDetails ("ro" ,"RO" ));
197+ localeList .put ("RSD" ,new LocaleDetails ("sr" ,"Latn_RS" ));
198+ localeList .put ("RUB" ,new LocaleDetails ("ru" ,"RU" ));
199+ localeList .put ("SBD" ,new LocaleDetails ("en" ,"SB" ));
200+ localeList .put ("SEK" ,new LocaleDetails ("en" ,"SE" ));
201+ localeList .put ("SGD" ,new LocaleDetails ("ta" ,"SG" ));
202+ localeList .put ("SVG" ,new LocaleDetails ("en" ,"US" ));
203+ localeList .put ("SZL" ,new LocaleDetails ("en" ,"SZ" ));
204+ localeList .put ("THB" ,new LocaleDetails ("th" ,"TH" ));
205+ localeList .put ("TND" ,new LocaleDetails ("en" ,"TN" ));
206+ localeList .put ("TOP" ,new LocaleDetails ("to" ,"TO" ));
207+ localeList .put ("TRY" ,new LocaleDetails ("tr" ,"TR" ));
208+ localeList .put ("TWD" ,new LocaleDetails ("zh" ,"TW" ));
209+ localeList .put ("UGX" ,new LocaleDetails ("cgg" ,"UG" ));
210+ localeList .put ("USD" ,new LocaleDetails ("es" ,"US" ));
211+ localeList .put ("UYU" ,new LocaleDetails ("es" ,"UY" ));
212+ localeList .put ("VND" ,new LocaleDetails ("vi" ,"VN" ));
213+ localeList .put ("VUV" ,new LocaleDetails ("en" ,"VU" ));
214+ localeList .put ("WST" ,new LocaleDetails ("en" ,"WS" ));
215+ localeList .put ("XPF" ,new LocaleDetails ("fr" ,"PF" ));
216+ localeList .put ("ZAR" ,new LocaleDetails ("en" ,"ZA" ));
217+ localeList .put ("ZMW" ,new LocaleDetails ("en" ,"ZM" ));
218+ }
219+
220+ public HashMap <String , LocaleDetails > getLocaleList ()
221+ {
222+ return localeList ;
223+ }
224+ /**
225+ * truncate decimals for given value
226+ *
227+ * @param value Any value in string.
228+ * @param noOfDecimals number of decimal to be truncate.
229+ * @return Returns truncated decimal value.
230+ */
231+ public static String getValueWithTruncateDecimals (String value , int noOfDecimals ) {
232+ if (value != null ) {
233+ NumberFormat nf = NumberFormat .getNumberInstance ();
234+ nf .setMaximumFractionDigits (noOfDecimals );
235+ nf .setRoundingMode (RoundingMode .HALF_UP );
236+ double amount = Double .parseDouble (value );
237+ return nf .format (amount );
238+ } else {
239+ return "" ;
240+ }
241+ }
130242}
0 commit comments