* @stable ICU 4.2
*/
public static final int PLURALCURRENCYSTYLE = 6;
+ /**
+ * {@icu} Constant to specify currency style of format which uses currency symbol
+ * to represent currency for accounting, for example: "($3.00), instead of
+ * "-$3.00" ({@link #CURRENCYSTYLE}).
+ * @draft ICU 53
+ * @provisional This API might change or be removed in a future release.
+ */
+ public static final int ACCOUNTINGCURRENCYSTYLE = 7;
/**
* Field constant used to construct a FieldPosition object. Signifies that
* @throws IllegalArgumentException if choice is not one of
* NUMBERSTYLE, CURRENCYSTYLE,
* PERCENTSTYLE, SCIENTIFICSTYLE,
- * INTEGERSTYLE,
- * ISOCURRENCYSTYLE, PLURALCURRENCYSTYLE,
+ * INTEGERSTYLE, ISOCURRENCYSTYLE,
+ * PLURALCURRENCYSTYLE and ACCOUNTSTYLE.
* @stable ICU 4.2
*/
public static NumberFormat getInstance(ULocale desiredLocale, int choice) {
- if (choice < NUMBERSTYLE || choice > PLURALCURRENCYSTYLE) {
+ if (choice < NUMBERSTYLE || choice > ACCOUNTINGCURRENCYSTYLE) {
throw new IllegalArgumentException(
"choice should be from NUMBERSTYLE to PLURALCURRENCYSTYLE");
}
// This style wont work for currency plural format.
// For currency plural format, the pattern is get from
// the locale (from CurrencyUnitPatterns) without override.
- if(choice == CURRENCYSTYLE || choice == ISOCURRENCYSTYLE){
+ if(choice == CURRENCYSTYLE || choice == ISOCURRENCYSTYLE || choice == ACCOUNTINGCURRENCYSTYLE){
String temp = symbols.getCurrencyPattern();
if(temp!=null){
pattern = temp;
* but by replacing the single currency sign with
* double currency sign or triple currency sign.
*/
- int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE :
- ((choice == ISOCURRENCYSTYLE || choice == PLURALCURRENCYSTYLE)?
- CURRENCYSTYLE : choice); //[Richard/GCL]
+ String patternKey = null;
+ switch (choice) {
+ case NUMBERSTYLE:
+ case INTEGERSTYLE:
+ patternKey = "decimalFormat";
+ break;
+ case CURRENCYSTYLE:
+ case ISOCURRENCYSTYLE:
+ case PLURALCURRENCYSTYLE:
+ patternKey = "currencyFormat";
+ break;
+ case PERCENTSTYLE:
+ patternKey = "percentFormat";
+ break;
+ case SCIENTIFICSTYLE:
+ patternKey = "scientificFormat";
+ break;
+ case ACCOUNTINGCURRENCYSTYLE:
+ patternKey = "accountingFormat";
+ break;
+ default:
+ assert false;
+ patternKey = "decimalFormat";
+ break;
+ }
ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.
getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, forLocale);
- String[] numberPatternKeys = { "decimalFormat", "currencyFormat", "percentFormat", "scientificFormat" };
NumberingSystem ns = NumberingSystem.getInstance(forLocale);
String result = null;
try {
- result = rb.getStringWithFallback("NumberElements/" + ns.getName() + "/patterns/"+numberPatternKeys[entry]);
+ result = rb.getStringWithFallback("NumberElements/" + ns.getName() + "/patterns/"+ patternKey);
} catch ( MissingResourceException ex ) {
- result = rb.getStringWithFallback("NumberElements/latn/patterns/"+numberPatternKeys[entry]);
+ result = rb.getStringWithFallback("NumberElements/latn/patterns/"+ patternKey);
}
return result;
*/
public void TestGetInstance() {
// Tests "public final static NumberFormat getInstance(int style)"
+ int maxStyle = NumberFormat.ACCOUNTINGCURRENCYSTYLE;
int[] invalid_cases = { NumberFormat.NUMBERSTYLE - 1, NumberFormat.NUMBERSTYLE - 2,
- NumberFormat.PLURALCURRENCYSTYLE + 1, NumberFormat.PLURALCURRENCYSTYLE + 2 };
+ maxStyle + 1, maxStyle + 2 };
- for (int i = NumberFormat.NUMBERSTYLE; i < NumberFormat.PLURALCURRENCYSTYLE; i++) {
+ for (int i = NumberFormat.NUMBERSTYLE; i < maxStyle; i++) {
try {
NumberFormat.getInstance(i);
} catch (Exception e) {
// Tests "public static NumberFormat getInstance(Locale inLocale, int style)"
String[] localeCases = { "en_US", "fr_FR", "de_DE", "jp_JP" };
- for (int i = NumberFormat.NUMBERSTYLE; i < NumberFormat.PLURALCURRENCYSTYLE; i++) {
+ for (int i = NumberFormat.NUMBERSTYLE; i < maxStyle; i++) {
for (int j = 0; j < localeCases.length; j++) {
try {
NumberFormat.getInstance(new Locale(localeCases[j]), i);
errln("FAIL: NumberFormat.getContext() does not return the value set, CAPITALIZATION_FOR_STANDALONE");
}
}
+
+ public void TestAccountingCurrency() {
+ String[][] tests = {
+ {"en_US", "1234.5", "$1,234.50", "true"},
+ {"en_US", "-1234.5", "($1,234.50)", "true"},
+ {"en_US", "0", "$0.00", "true"},
+ {"en_US", "-0.2", "($0.20)", "true"},
+ {"ja_JP", "10000", "¥10,000", "true"},
+ {"ja_JP", "-1000.5", "(¥1,000)", "false"},
+ {"de_DE", "-23456.7", "-23.456,70\u00A0€", "true"},
+ };
+ for (String[] data : tests) {
+ ULocale loc = new ULocale(data[0]);
+ double num = Double.parseDouble(data[1]);
+ String fmt = data[2];
+ boolean rt = Boolean.parseBoolean(data[3]);
+
+ NumberFormat acfmt = NumberFormat.getInstance(loc, NumberFormat.ACCOUNTINGCURRENCYSTYLE);
+ expect(acfmt, num, fmt, rt);
+ }
+ }
}