I have used Calender.getAvailableLocales()
method to find which locales are available and from that have chosen 4. For each of these I want to output the same date, number, price and percentage to show the differences between the locales.
I have created the locale objects and their constructors as shown below. I have also created a NumberFormat
and a DateFormat
instance.
Locale aLocale = new Locale.Builder().setLanguage("fr").setRegion("CA").build();
Locale bLocale = new Locale.Builder().setLanguage("en").setRegion("US").build();
Locale cLocale = new Locale.Builder().setLanguage("en").setRegion("GB").build();
Locale dLocale = new Locale.Builder().setLanguage("ru").setScript("Cyrl").build();
aLocale = new Locale("fr", "CA");
bLocale = new Locale("en", "US");
cLocale = new Locale("en", "GB");
dLocale = new Locale("ru");
NumberFormat nf = NumberFormat.getInstance();
DateFormat df = DateFormat.getInstance();
The problem I am having is how I actually make the method call to the NumberFormat
to output the various values I want. I assume I need to use the instance I have created somehow with the locales that I created but I cannot figure out how. Any help will be greatly appreciated. Just a pointer and not a whole solution please.
Based on the suggestions made, I am now using this style to show the differences.
NumberFormat aNumber = NumberFormat.getNumberInstance(aLocale);
NumberFormat aPercent = NumberFormat.getPercentInstance(aLocale);
NumberFormat aPrice = NumberFormat.getCurrencyInstance(aLocale);
NumberFormat bNumber = NumberFormat.getNumberInstance(bLocale);
NumberFormat bPercent = NumberFormat.getPercentInstance(bLocale);
NumberFormat bPrice = NumberFormat.getCurrencyInstance(bLocale);
System.out.println(aNumber.format(9257476));
System.out.println(aPercent.format(63));
System.out.println(aPrice.format(684));
System.out.println(bNumber.format(9257476));
System.out.println(bPercent.format(63));
System.out.println(bPrice.format(684));
Which outputs this:
9 257 476
6 300 %
684,00 $
9,257,476
6,300%
$684.00
I tried doing a similar thing for the date but the method doesn't allow for a locale to be passed as a parameter. I need to make sure the correct locale is being used each time so I'm not sure how to adjust for this?
Are you looking for this.
NumberFormat currency = NumberFormat.getCurrencyInstance(aLocale);
NumberFormat percent = NumberFormat.getPercentInstance(aLocale);
NumberFormat number = NumberFormat.getNumberInstance(aLocale);
int style = DateFormat.MEDIUM;
DateFormat df = DateFormat.getInstance(style,aLocale);
eg
NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse("$19,67,456.45");
System.out.println(number.toString());
System.out.println(format.format(Double.valueOf(number.toString())));
output
1967456.45
$1,967,456.45
Edit
Use SimpleDateFormat for date with specified format.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", aLocale);
System.out.println(dateFormat.parse("12/03/1993"));