OLD | NEW |
1 // © 2016 and later: Unicode, Inc. and others. | 1 // © 2016 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 /* | 3 /* |
4 ******************************************************************************* | 4 ******************************************************************************* |
5 * Copyright (C) 2001-2016, International Business Machines Corporation and | 5 * Copyright (C) 2001-2016, International Business Machines Corporation and |
6 * others. All Rights Reserved. | 6 * others. All Rights Reserved. |
7 ******************************************************************************* | 7 ******************************************************************************* |
8 */ | 8 */ |
9 | 9 |
10 /** | 10 /** |
(...skipping 802 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
813 | 813 |
814 | 814 |
815 @Test | 815 @Test |
816 public void TestMiscCurrencyParsing() { | 816 public void TestMiscCurrencyParsing() { |
817 String[][] DATA = { | 817 String[][] DATA = { |
818 // each has: string to be parsed, parsed position, error positio
n | 818 // each has: string to be parsed, parsed position, error positio
n |
819 {"1.00 ", "4", "-1", "0", "4"}, | 819 {"1.00 ", "4", "-1", "0", "4"}, |
820 {"1.00 UAE dirha", "4", "-1", "0", "4"}, | 820 {"1.00 UAE dirha", "4", "-1", "0", "4"}, |
821 {"1.00 us dollar", "14", "-1", "14", "-1"}, | 821 {"1.00 us dollar", "14", "-1", "14", "-1"}, |
822 {"1.00 US DOLLAR", "14", "-1", "14", "-1"}, | 822 {"1.00 US DOLLAR", "14", "-1", "14", "-1"}, |
823 {"1.00 usd", "8", "-1", "8", "-1"}, | 823 {"1.00 usd", "4", "-1", "8", "-1"}, |
824 {"1.00 USD", "8", "-1", "8", "-1"}, | 824 {"1.00 USD", "8", "-1", "8", "-1"}, |
825 }; | 825 }; |
826 ULocale locale = new ULocale("en_US"); | 826 ULocale locale = new ULocale("en_US"); |
827 for (int i=0; i<DATA.length; ++i) { | 827 for (int i=0; i<DATA.length; ++i) { |
828 String stringToBeParsed = DATA[i][0]; | 828 String stringToBeParsed = DATA[i][0]; |
829 int parsedPosition = Integer.parseInt(DATA[i][1]); | 829 int parsedPosition = Integer.parseInt(DATA[i][1]); |
830 int errorIndex = Integer.parseInt(DATA[i][2]); | 830 int errorIndex = Integer.parseInt(DATA[i][2]); |
831 int currParsedPosition = Integer.parseInt(DATA[i][3]); | 831 int currParsedPosition = Integer.parseInt(DATA[i][3]); |
832 int currErrorIndex = Integer.parseInt(DATA[i][4]); | 832 int currErrorIndex = Integer.parseInt(DATA[i][4]); |
833 NumberFormat numFmt = NumberFormat.getInstance(locale, NumberFormat.
CURRENCYSTYLE); | 833 NumberFormat numFmt = NumberFormat.getInstance(locale, NumberFormat.
CURRENCYSTYLE); |
(...skipping 21 matching lines...) Expand all Loading... |
855 } | 855 } |
856 } | 856 } |
857 } | 857 } |
858 | 858 |
859 @Test | 859 @Test |
860 public void TestParseCurrency() { | 860 public void TestParseCurrency() { |
861 class ParseCurrencyItem { | 861 class ParseCurrencyItem { |
862 private final String localeString; | 862 private final String localeString; |
863 private final String descrip; | 863 private final String descrip; |
864 private final String currStr; | 864 private final String currStr; |
| 865 private final int doubExpectPos; |
| 866 private final int doubExpectVal; |
865 private final int curExpectPos; | 867 private final int curExpectPos; |
866 private final int curExpectVal; | 868 private final int curExpectVal; |
867 private final String curExpectCurr; | 869 private final String curExpectCurr; |
868 | 870 |
869 ParseCurrencyItem(String locStr, String desc, String curr, int curEx
Pos, int curExVal, String curExCurr) { | 871 ParseCurrencyItem(String locStr, String desc, String curr, int doubE
xPos, int doubExVal, int curExPos, int curExVal, String curExCurr) { |
870 localeString = locStr; | 872 localeString = locStr; |
871 descrip = desc; | 873 descrip = desc; |
872 currStr = curr; | 874 currStr = curr; |
| 875 doubExpectPos = doubExPos; |
| 876 doubExpectVal = doubExVal; |
873 curExpectPos = curExPos; | 877 curExpectPos = curExPos; |
874 curExpectVal = curExVal; | 878 curExpectVal = curExVal; |
875 curExpectCurr = curExCurr; | 879 curExpectCurr = curExCurr; |
876 } | 880 } |
877 public String getLocaleString() { return localeString; } | 881 public String getLocaleString() { return localeString; } |
878 public String getDescrip() { return descrip; } | 882 public String getDescrip() { return descrip; } |
879 public String getCurrStr() { return currStr; } | 883 public String getCurrStr() { return currStr; } |
| 884 public int getDoubExpectPos() { return doubExpectPos; } |
| 885 public int getDoubExpectVal() { return doubExpectVal; } |
880 public int getCurExpectPos() { return curExpectPos; } | 886 public int getCurExpectPos() { return curExpectPos; } |
881 public int getCurExpectVal() { return curExpectVal; } | 887 public int getCurExpectVal() { return curExpectVal; } |
882 public String getCurExpectCurr() { return curExpectCurr; } | 888 public String getCurExpectCurr() { return curExpectCurr; } |
883 } | 889 } |
884 // Note: In cases where the number occurs before the currency sign, non-
currency mode will parse the number | 890 // Note: In cases where the number occurs before the currency sign, non-
currency mode will parse the number |
885 // and stop when it reaches the currency symbol. | 891 // and stop when it reaches the currency symbol. |
886 final ParseCurrencyItem[] parseCurrencyItems = { | 892 final ParseCurrencyItem[] parseCurrencyItems = { |
887 new ParseCurrencyItem( "en_US", "dollars2", "$2.00",
5, 2, "USD" ), | 893 new ParseCurrencyItem( "en_US", "dollars2", "$2.00",
5, 2, 5, 2, "USD" ), |
888 new ParseCurrencyItem( "en_US", "dollars4", "$4",
2, 4, "USD" ), | 894 new ParseCurrencyItem( "en_US", "dollars4", "$4",
2, 4, 2, 4, "USD" ), |
889 new ParseCurrencyItem( "en_US", "dollars9", "9\u00A0$",
3, 9, "USD" ), | 895 new ParseCurrencyItem( "en_US", "dollars9", "9\u00A0$",
3, 9, 3, 9, "USD" ), |
890 new ParseCurrencyItem( "en_US", "pounds3", "\u00A33.00",
5, 3, "GBP" ), | 896 new ParseCurrencyItem( "en_US", "pounds3", "\u00A33.00",
0, 0, 5, 3, "GBP" ), |
891 new ParseCurrencyItem( "en_US", "pounds5", "\u00A35",
2, 5, "GBP" ), | 897 new ParseCurrencyItem( "en_US", "pounds5", "\u00A35",
0, 0, 2, 5, "GBP" ), |
892 new ParseCurrencyItem( "en_US", "pounds7", "7\u00A0\u00A3",
3, 7, "GBP" ), | 898 new ParseCurrencyItem( "en_US", "pounds7", "7\u00A0\u00A3",
1, 7, 3, 7, "GBP" ), |
893 new ParseCurrencyItem( "en_US", "euros8", "\u20AC8",
2, 8, "EUR" ), | 899 new ParseCurrencyItem( "en_US", "euros8", "\u20AC8",
0, 0, 2, 8, "EUR" ), |
894 | 900 |
895 new ParseCurrencyItem( "en_GB", "pounds3", "\u00A33.00",
5, 3, "GBP" ), | 901 new ParseCurrencyItem( "en_GB", "pounds3", "\u00A33.00",
5, 3, 5, 3, "GBP" ), |
896 new ParseCurrencyItem( "en_GB", "pounds5", "\u00A35",
2, 5, "GBP" ), | 902 new ParseCurrencyItem( "en_GB", "pounds5", "\u00A35",
2, 5, 2, 5, "GBP" ), |
897 new ParseCurrencyItem( "en_GB", "pounds7", "7\u00A0\u00A3",
3, 7, "GBP" ), | 903 new ParseCurrencyItem( "en_GB", "pounds7", "7\u00A0\u00A3",
3, 7, 3, 7, "GBP" ), |
898 new ParseCurrencyItem( "en_GB", "euros4", "4,00\u00A0\u20AC",
6,400, "EUR" ), | 904 new ParseCurrencyItem( "en_GB", "euros4", "4,00\u00A0\u20AC",
4,400, 6,400, "EUR" ), |
899 new ParseCurrencyItem( "en_GB", "euros6", "6\u00A0\u20AC",
3, 6, "EUR" ), | 905 new ParseCurrencyItem( "en_GB", "euros6", "6\u00A0\u20AC",
1, 6, 3, 6, "EUR" ), |
900 new ParseCurrencyItem( "en_GB", "euros8", "\u20AC8",
2, 8, "EUR" ), | 906 new ParseCurrencyItem( "en_GB", "euros8", "\u20AC8",
0, 0, 2, 8, "EUR" ), |
901 new ParseCurrencyItem( "en_GB", "dollars4", "US$4",
4, 4, "USD" ), | 907 new ParseCurrencyItem( "en_GB", "dollars4", "US$4",
0, 0, 4, 4, "USD" ), |
902 | 908 |
903 new ParseCurrencyItem( "fr_FR", "euros4", "4,00\u00A0\u20AC",
6, 4, "EUR" ), | 909 new ParseCurrencyItem( "fr_FR", "euros4", "4,00\u00A0\u20AC",
6, 4, 6, 4, "EUR" ), |
904 new ParseCurrencyItem( "fr_FR", "euros6", "6\u00A0\u20AC",
3, 6, "EUR" ), | 910 new ParseCurrencyItem( "fr_FR", "euros6", "6\u00A0\u20AC",
3, 6, 3, 6, "EUR" ), |
905 new ParseCurrencyItem( "fr_FR", "euros8", "\u20AC8",
2, 8, "EUR" ), | 911 new ParseCurrencyItem( "fr_FR", "euros8", "\u20AC8",
2, 8, 2, 8, "EUR" ), |
906 new ParseCurrencyItem( "fr_FR", "dollars2", "$2.00",
0, 0, "" ), | 912 new ParseCurrencyItem( "fr_FR", "dollars2", "$2.00",
0, 0, 0, 0, "" ), |
907 new ParseCurrencyItem( "fr_FR", "dollars4", "$4",
0, 0, "" ), | 913 new ParseCurrencyItem( "fr_FR", "dollars4", "$4",
0, 0, 0, 0, "" ), |
908 }; | 914 }; |
909 for (ParseCurrencyItem item: parseCurrencyItems) { | 915 for (ParseCurrencyItem item: parseCurrencyItems) { |
910 String localeString = item.getLocaleString(); | 916 String localeString = item.getLocaleString(); |
911 ULocale uloc = new ULocale(localeString); | 917 ULocale uloc = new ULocale(localeString); |
912 NumberFormat fmt = null; | 918 NumberFormat fmt = null; |
913 try { | 919 try { |
914 fmt = NumberFormat.getCurrencyInstance(uloc); | 920 fmt = NumberFormat.getCurrencyInstance(uloc); |
915 } catch (Exception e) { | 921 } catch (Exception e) { |
916 errln("NumberFormat.getCurrencyInstance fails for locale " + loc
aleString); | 922 errln("NumberFormat.getCurrencyInstance fails for locale " + loc
aleString); |
917 continue; | 923 continue; |
918 } | 924 } |
919 String currStr = item.getCurrStr(); | 925 String currStr = item.getCurrStr(); |
920 ParsePosition parsePos = new ParsePosition(0); | 926 ParsePosition parsePos = new ParsePosition(0); |
921 | 927 |
922 Number numVal = fmt.parse(currStr, parsePos); | 928 Number numVal = fmt.parse(currStr, parsePos); |
923 if ( parsePos.getIndex() != item.getCurExpectPos() || (numVal != nul
l && numVal.intValue() != item.getCurExpectVal()) ) { | 929 if ( parsePos.getIndex() != item.getDoubExpectPos() || (numVal != nu
ll && numVal.intValue() != item.getDoubExpectVal()) ) { |
924 if (numVal != null) { | 930 if (numVal != null) { |
925 errln("NumberFormat.getCurrencyInstance parse " + localeStri
ng + "/" + item.getDescrip() + | 931 errln("NumberFormat.getCurrencyInstance parse " + localeStri
ng + "/" + item.getDescrip() + |
926 ", expect pos/val " + item.getCurExpectPos() + "/" +
item.getCurExpectVal() + | 932 ", expect pos/val " + item.getDoubExpectPos() + "/"
+ item.getDoubExpectVal() + |
927 ", get " + parsePos.getIndex() + "/" + numVal.intVal
ue() ); | 933 ", get " + parsePos.getIndex() + "/" + numVal.intVal
ue() ); |
928 } else { | 934 } else { |
929 errln("NumberFormat.getCurrencyInstance parse " + localeStri
ng + "/" + item.getDescrip() + | 935 errln("NumberFormat.getCurrencyInstance parse " + localeStri
ng + "/" + item.getDescrip() + |
930 ", expect pos/val " + item.getCurExpectPos() + "/" +
item.getCurExpectVal() + | 936 ", expect pos/val " + item.getDoubExpectPos() + "/"
+ item.getDoubExpectVal() + |
931 ", get " + parsePos.getIndex() + "/(NULL)" ); | 937 ", get " + parsePos.getIndex() + "/(NULL)" ); |
932 } | 938 } |
933 } | 939 } |
934 | 940 |
935 parsePos.setIndex(0); | 941 parsePos.setIndex(0); |
936 int curExpectPos = item.getCurExpectPos(); | 942 int curExpectPos = item.getCurExpectPos(); |
937 CurrencyAmount currAmt = fmt.parseCurrency(currStr, parsePos); | 943 CurrencyAmount currAmt = fmt.parseCurrency(currStr, parsePos); |
938 if ( parsePos.getIndex() != curExpectPos || (currAmt != null && (cur
rAmt.getNumber().intValue() != item.getCurExpectVal() || | 944 if ( parsePos.getIndex() != curExpectPos || (currAmt != null && (cur
rAmt.getNumber().intValue() != item.getCurExpectVal() || |
939 currAmt.getCurrency().getCurrencyCode().compareTo(item.getCu
rExpectCurr()) != 0)) ) { | 945 currAmt.getCurrency().getCurrencyCode().compareTo(item.getCu
rExpectCurr()) != 0)) ) { |
940 if (currAmt != null) { | 946 if (currAmt != null) { |
(...skipping 5103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6044 assertEquals("Should consume whole string", 3, ppos.getIndex()); | 6050 assertEquals("Should consume whole string", 3, ppos.getIndex()); |
6045 } | 6051 } |
6046 | 6052 |
6047 @Test | 6053 @Test |
6048 public void testStrictParseCurrencyLongNames() { | 6054 public void testStrictParseCurrencyLongNames() { |
6049 DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(ULocale.ENG
LISH, DecimalFormat.PLURALCURRENCYSTYLE); | 6055 DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(ULocale.ENG
LISH, DecimalFormat.PLURALCURRENCYSTYLE); |
6050 df.setParseStrict(true); | 6056 df.setParseStrict(true); |
6051 df.setCurrency(Currency.getInstance("USD")); | 6057 df.setCurrency(Currency.getInstance("USD")); |
6052 double input = 514.23; | 6058 double input = 514.23; |
6053 String formatted = df.format(input); | 6059 String formatted = df.format(input); |
6054 assertEquals("Should format as expected", "514.23 US dollars", formatted
); | 6060 String expected = "514.23 US dollars"; |
| 6061 assertEquals("Should format as expected", expected, formatted); |
6055 ParsePosition ppos = new ParsePosition(0); | 6062 ParsePosition ppos = new ParsePosition(0); |
6056 CurrencyAmount ca = df.parseCurrency(formatted, ppos); | 6063 CurrencyAmount ca = df.parseCurrency(formatted, ppos); |
6057 assertEquals("Should consume whole number", ppos.getIndex(), 17); | 6064 assertEquals("Should consume whole number", ppos.getIndex(), 17); |
6058 assertEquals("Number should round-trip", ca.getNumber().doubleValue(), i
nput); | 6065 assertEquals("Number should round-trip", ca.getNumber().doubleValue(), i
nput); |
6059 assertEquals("Should get correct currency", ca.getCurrency().getCurrency
Code(), "USD"); | 6066 assertEquals("Should get correct currency", ca.getCurrency().getCurrency
Code(), "USD"); |
| 6067 // Should also round-trip in non-currency parsing |
| 6068 expect2(df, input, expected); |
6060 } | 6069 } |
6061 | 6070 |
6062 @Test | 6071 @Test |
6063 public void testStrictParseCurrencySpacing() { | 6072 public void testStrictParseCurrencySpacing() { |
6064 DecimalFormat df = new DecimalFormat("¤ 0", DecimalFormatSymbols.getInst
ance(ULocale.ROOT)); | 6073 DecimalFormat df = new DecimalFormat("¤ 0", DecimalFormatSymbols.getInst
ance(ULocale.ROOT)); |
6065 df.setCurrency(Currency.getInstance("USD")); | 6074 df.setCurrency(Currency.getInstance("USD")); |
6066 df.setParseStrict(true); | 6075 df.setParseStrict(true); |
6067 expect2(df, -51.42, "-US$ 51.42"); | 6076 expect2(df, -51.42, "-US$ 51.42"); |
6068 } | 6077 } |
6069 | 6078 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6138 public void test11897_LocalizedPatternSeparator() { | 6147 public void test11897_LocalizedPatternSeparator() { |
6139 DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(ULocale.ENGL
ISH); | 6148 DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(ULocale.ENGL
ISH); |
6140 dfs.setPatternSeparator('!'); | 6149 dfs.setPatternSeparator('!'); |
6141 DecimalFormat df = new DecimalFormat("0", dfs); | 6150 DecimalFormat df = new DecimalFormat("0", dfs); |
6142 df.applyPattern("a0;b0"); // should not throw | 6151 df.applyPattern("a0;b0"); // should not throw |
6143 assertEquals("should apply the normal pattern", df.getNegativePrefix(),
"b"); | 6152 assertEquals("should apply the normal pattern", df.getNegativePrefix(),
"b"); |
6144 df.applyLocalizedPattern("c0!d0"); // should not throw | 6153 df.applyLocalizedPattern("c0!d0"); // should not throw |
6145 assertEquals("should apply the localized pattern", df.getNegativePrefix(
), "d"); | 6154 assertEquals("should apply the localized pattern", df.getNegativePrefix(
), "d"); |
6146 } | 6155 } |
6147 } | 6156 } |
OLD | NEW |