use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestCases.
// InputStream is will be closed by the ResourceReader.
@SuppressWarnings("resource")
@Test
public void TestCases() {
String caseFileName = "NumberFormatTestCases.txt";
java.io.InputStream is = NumberFormatTest.class.getResourceAsStream(caseFileName);
ResourceReader reader = new ResourceReader(is, caseFileName, "utf-8");
TokenIterator tokens = new TokenIterator(reader);
Locale loc = new Locale("en", "US", "");
DecimalFormat ref = null, fmt = null;
MeasureFormat mfmt = null;
String pat = null, str = null, mloc = null;
boolean strict = false;
try {
for (; ; ) {
String tok = tokens.next();
if (tok == null) {
break;
}
String where = "(" + tokens.getLineNumber() + ") ";
int cmd = keywordIndex(tok);
switch(cmd) {
case 0:
// ref= <reference pattern>
ref = new DecimalFormat(tokens.next(), new DecimalFormatSymbols(Locale.US));
ref.setParseStrict(strict);
logln("Setting reference pattern to:\t" + ref);
break;
case 1:
// loc= <locale>
loc = LocaleUtility.getLocaleFromName(tokens.next());
pat = ((DecimalFormat) NumberFormat.getInstance(loc)).toPattern();
logln("Setting locale to:\t" + loc + ", \tand pattern to:\t" + pat);
break;
// f:
case 2:
// fp:
case 3:
// rt:
case 4:
case // p:
5:
tok = tokens.next();
if (!tok.equals("-")) {
pat = tok;
}
try {
fmt = new DecimalFormat(pat, new DecimalFormatSymbols(loc));
fmt.setParseStrict(strict);
} catch (IllegalArgumentException iae) {
errln(where + "Pattern \"" + pat + '"');
iae.printStackTrace();
// consume remaining tokens
tokens.next();
// tokens.next();
if (cmd == 3)
tokens.next();
continue;
}
str = null;
try {
if (cmd == 2 || cmd == 3 || cmd == 4) {
// f: <pattern or '-'> <number> <exp. string>
// fp: <pattern or '-'> <number> <exp. string> <exp. number>
// rt: <pattern or '-'> <number> <string>
String num = tokens.next();
str = tokens.next();
Number n = ref.parse(num);
assertEquals(where + '"' + pat + "\".format(" + num + ")", str, fmt.format(n));
if (cmd == 3) {
// fp:
n = ref.parse(tokens.next());
}
if (cmd != 2) {
// != f:
assertEquals(where + '"' + pat + "\".parse(\"" + str + "\")", n, fmt.parse(str));
}
} else // p: <pattern or '-'> <string to parse> <exp. number>
{
str = tokens.next();
String expstr = tokens.next();
Number parsed = fmt.parse(str);
Number exp = ref.parse(expstr);
assertEquals(where + '"' + pat + "\".parse(\"" + str + "\")", exp, parsed);
}
} catch (ParseException e) {
errln(where + '"' + pat + "\".parse(\"" + str + "\") threw an exception");
e.printStackTrace();
}
break;
case 6:
// perr: <pattern or '-'> <invalid string>
errln("Under construction");
return;
case 7:
// pat: <pattern> <exp. toPattern, or '-' or 'err'>
String testpat = tokens.next();
String exppat = tokens.next();
boolean err = exppat.equals("err");
if (testpat.equals("-")) {
if (err) {
errln("Invalid command \"pat: - err\" at " + tokens.describePosition());
continue;
}
testpat = pat;
}
if (exppat.equals("-"))
exppat = testpat;
try {
DecimalFormat f = null;
if (testpat == pat) {
// [sic]
f = fmt;
} else {
f = new DecimalFormat(testpat);
f.setParseStrict(strict);
}
if (err) {
errln(where + "Invalid pattern \"" + testpat + "\" was accepted");
} else {
assertEquals(where + '"' + testpat + "\".toPattern()", exppat, f.toPattern());
}
} catch (IllegalArgumentException iae2) {
if (err) {
logln("Ok: " + where + "Invalid pattern \"" + testpat + "\" threw an exception");
} else {
errln(where + "Valid pattern \"" + testpat + "\" threw an exception");
iae2.printStackTrace();
}
}
break;
case // fpc:
8:
tok = tokens.next();
if (!tok.equals("-")) {
mloc = tok;
ULocale l = new ULocale(mloc);
try {
mfmt = MeasureFormat.getCurrencyFormat(l);
} catch (IllegalArgumentException iae) {
errln(where + "Loc \"" + tok + '"');
iae.printStackTrace();
// consume remaining tokens
tokens.next();
tokens.next();
tokens.next();
continue;
}
}
str = null;
try {
// fpc: <loc or '-'> <curr.amt> <exp. string> <exp. curr.amt>
String currAmt = tokens.next();
str = tokens.next();
CurrencyAmount target = parseCurrencyAmount(currAmt, ref, '/');
String formatResult = mfmt.format(target);
assertEquals(where + "getCurrencyFormat(" + mloc + ").format(" + currAmt + ")", str, formatResult);
target = parseCurrencyAmount(tokens.next(), ref, '/');
CurrencyAmount parseResult = (CurrencyAmount) mfmt.parseObject(str);
assertEquals(where + "getCurrencyFormat(" + mloc + ").parse(\"" + str + "\")", target, parseResult);
} catch (ParseException e) {
errln(where + '"' + pat + "\".parse(\"" + str + "\") threw an exception");
e.printStackTrace();
}
break;
case // strict= true or false
9:
strict = "true".equalsIgnoreCase(tokens.next());
logln("Setting strict to:\t" + strict);
break;
case -1:
errln("Unknown command \"" + tok + "\" at " + tokens.describePosition());
return;
}
}
} catch (java.io.IOException e) {
throw new RuntimeException(e);
} finally {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestMultiCurrencySign.
@Test
public void TestMultiCurrencySign() {
String[][] DATA = { // for US locale
{ "en_US", "\u00A4#,##0.00;-\u00A4#,##0.00", "1234.56", "$1,234.56", "USD1,234.56", "US dollars1,234.56" }, { "en_US", "\u00A4#,##0.00;-\u00A4#,##0.00", "-1234.56", "-$1,234.56", "-USD1,234.56", "-US dollars1,234.56" }, { "en_US", "\u00A4#,##0.00;-\u00A4#,##0.00", "1", "$1.00", "USD1.00", "US dollars1.00" }, // for CHINA locale
{ "zh_CN", "\u00A4#,##0.00;(\u00A4#,##0.00)", "1234.56", "\uFFE51,234.56", "CNY1,234.56", "\u4EBA\u6C11\u5E011,234.56" }, { "zh_CN", "\u00A4#,##0.00;(\u00A4#,##0.00)", "-1234.56", "(\uFFE51,234.56)", "(CNY1,234.56)", "(\u4EBA\u6C11\u5E011,234.56)" }, { "zh_CN", "\u00A4#,##0.00;(\u00A4#,##0.00)", "1", "\uFFE51.00", "CNY1.00", "\u4EBA\u6C11\u5E011.00" } };
String doubleCurrencyStr = "\u00A4\u00A4";
String tripleCurrencyStr = "\u00A4\u00A4\u00A4";
for (int i = 0; i < DATA.length; ++i) {
String locale = DATA[i][0];
String pat = DATA[i][1];
Double numberToBeFormat = new Double(DATA[i][2]);
DecimalFormatSymbols sym = new DecimalFormatSymbols(new ULocale(locale));
for (int j = 1; j <= 3; ++j) {
// j represents the number of currency sign in the pattern.
if (j == 2) {
pat = pat.replaceAll("\u00A4", doubleCurrencyStr);
} else if (j == 3) {
pat = pat.replaceAll("\u00A4\u00A4", tripleCurrencyStr);
}
DecimalFormat fmt = new DecimalFormat(pat, sym);
String s = ((NumberFormat) fmt).format(numberToBeFormat);
// DATA[i][3] is the currency format result using a
// single currency sign.
// DATA[i][4] is the currency format result using
// double currency sign.
// DATA[i][5] is the currency format result using
// triple currency sign.
// DATA[i][j+2] is the currency format result using
// 'j' number of currency sign.
String currencyFormatResult = DATA[i][2 + j];
if (!s.equals(currencyFormatResult)) {
errln("FAIL format: Expected " + currencyFormatResult);
}
try {
// mix style parsing
for (int k = 3; k <= 5; ++k) {
// DATA[i][3] is the currency format result using a
// single currency sign.
// DATA[i][4] is the currency format result using
// double currency sign.
// DATA[i][5] is the currency format result using
// triple currency sign.
String oneCurrencyFormat = DATA[i][k];
if (fmt.parse(oneCurrencyFormat).doubleValue() != numberToBeFormat.doubleValue()) {
errln("FAILED parse " + oneCurrencyFormat);
}
}
} catch (ParseException e) {
errln("FAILED, DecimalFormat parse currency: " + e.toString());
}
}
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestShowZero.
@Test
public void TestShowZero() {
DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(ULocale.US);
numberFormat.setSignificantDigitsUsed(true);
numberFormat.setMaximumSignificantDigits(3);
assertEquals("TestShowZero", "0", numberFormat.format(0.0));
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestSignificantDigits.
@Test
public void TestSignificantDigits() {
double[] input = { 0, 0, 123, -123, 12345, -12345, 123.45, -123.45, 123.44501, -123.44501, 0.001234, -0.001234, 0.00000000123, -0.00000000123, 0.0000000000000000000123, -0.0000000000000000000123, 1.2, -1.2, 0.0000000012344501, -0.0000000012344501, 123445.01, -123445.01, 12344501000000000000000000000000000.0, -12344501000000000000000000000000000.0 };
String[] expected = { "0.00", "0.00", "123", "-123", "12345", "-12345", "123.45", "-123.45", "123.45", "-123.45", "0.001234", "-0.001234", "0.00000000123", "-0.00000000123", "0.0000000000000000000123", "-0.0000000000000000000123", "1.20", "-1.20", "0.0000000012345", "-0.0000000012345", "123450", "-123450", "12345000000000000000000000000000000", "-12345000000000000000000000000000000" };
DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(ULocale.US);
numberFormat.setSignificantDigitsUsed(true);
numberFormat.setMinimumSignificantDigits(3);
numberFormat.setMaximumSignificantDigits(5);
numberFormat.setGroupingUsed(false);
for (int i = 0; i < input.length; i++) {
assertEquals("TestSignificantDigits", expected[i], numberFormat.format(input[i]));
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method expectCurrency.
private void expectCurrency(NumberFormat nf, Currency curr, double value, String string) {
DecimalFormat fmt = (DecimalFormat) nf;
if (curr != null) {
fmt.setCurrency(curr);
}
String s = fmt.format(value).replace('\u00A0', ' ');
if (s.equals(string)) {
logln("Ok: " + value + " x " + curr + " => " + s);
} else {
errln("FAIL: " + value + " x " + curr + " => " + s + ", expected " + string);
}
}
Aggregations