use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestFieldPositionFractionButInteger.
@Test
public void TestFieldPositionFractionButInteger() {
DecimalFormat nf = (DecimalFormat) android.icu.text.NumberFormat.getInstance(ULocale.ENGLISH);
nf.setPositivePrefix("FOO");
nf.setPositiveSuffix("BA");
StringBuffer buffer = new StringBuffer();
FieldPosition fp = new FieldPosition(NumberFormat.Field.FRACTION);
nf.format(35, buffer, fp);
assertEquals("35", "FOO35BA", buffer.toString());
assertEquals("fp begin", 5, fp.getBeginIndex());
assertEquals("fp end", 5, fp.getEndIndex());
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestCoverage.
@Test
public void TestCoverage() {
// default locale
NumberFormat fmt = NumberFormat.getNumberInstance();
logln(fmt.format(new BigInteger("1234567890987654321234567890987654321", 10)));
// default locale
fmt = NumberFormat.getScientificInstance();
logln(fmt.format(PI.INSTANCE));
try {
logln(fmt.format("12345"));
errln("numberformat of string did not throw exception");
} catch (Exception e) {
logln("PASS: numberformat of string failed as expected");
}
int hash = fmt.hashCode();
logln("hash code " + hash);
logln("compare to string returns: " + fmt.equals(""));
// For ICU 2.6 - alan
DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
DecimalFormat df = new DecimalFormat("'*&'' '\u00A4' ''&*' #,##0.00", US);
df.setCurrency(Currency.getInstance("INR"));
expect2(df, 1.0, "*&' \u20B9 '&* 1.00");
expect2(df, -2.0, "-*&' \u20B9 '&* 2.00");
df.applyPattern("#,##0.00 '*&'' '\u00A4' ''&*'");
expect2(df, 2.0, "2.00 *&' \u20B9 '&*");
expect2(df, -1.0, "-1.00 *&' \u20B9 '&*");
java.math.BigDecimal r;
r = df.getRoundingIncrement();
if (r != null) {
errln("FAIL: rounding = " + r + ", expect null");
}
if (df.isScientificNotation()) {
errln("FAIL: isScientificNotation = true, expect false");
}
df.applyPattern("0.00000");
df.setScientificNotation(true);
if (!df.isScientificNotation()) {
errln("FAIL: isScientificNotation = false, expect true");
}
df.setMinimumExponentDigits((byte) 2);
if (df.getMinimumExponentDigits() != 2) {
errln("FAIL: getMinimumExponentDigits = " + df.getMinimumExponentDigits() + ", expect 2");
}
df.setExponentSignAlwaysShown(true);
if (!df.isExponentSignAlwaysShown()) {
errln("FAIL: isExponentSignAlwaysShown = false, expect true");
}
df.setSecondaryGroupingSize(0);
if (df.getSecondaryGroupingSize() != 0) {
errln("FAIL: getSecondaryGroupingSize = " + df.getSecondaryGroupingSize() + ", expect 0");
}
expect2(df, 3.14159, "3.14159E+00");
// DecimalFormatSymbols#getInstance
DecimalFormatSymbols decsym1 = DecimalFormatSymbols.getInstance();
DecimalFormatSymbols decsym2 = new DecimalFormatSymbols();
if (!decsym1.equals(decsym2)) {
errln("FAIL: DecimalFormatSymbols returned by getInstance()" + "does not match new DecimalFormatSymbols().");
}
decsym1 = DecimalFormatSymbols.getInstance(Locale.JAPAN);
decsym2 = DecimalFormatSymbols.getInstance(ULocale.JAPAN);
if (!decsym1.equals(decsym2)) {
errln("FAIL: DecimalFormatSymbols returned by getInstance(Locale.JAPAN)" + "does not match the one returned by getInstance(ULocale.JAPAN).");
}
// DecimalFormatSymbols#getAvailableLocales/#getAvailableULocales
Locale[] allLocales = DecimalFormatSymbols.getAvailableLocales();
if (allLocales.length == 0) {
errln("FAIL: Got a empty list for DecimalFormatSymbols.getAvailableLocales");
} else {
logln("PASS: " + allLocales.length + " available locales returned by DecimalFormatSymbols.getAvailableLocales");
}
ULocale[] allULocales = DecimalFormatSymbols.getAvailableULocales();
if (allULocales.length == 0) {
errln("FAIL: Got a empty list for DecimalFormatSymbols.getAvailableLocales");
} else {
logln("PASS: " + allULocales.length + " available locales returned by DecimalFormatSymbols.getAvailableULocales");
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestParseNegativeWithFaLocale.
@Test
public void TestParseNegativeWithFaLocale() {
DecimalFormat parser = (DecimalFormat) NumberFormat.getInstance(new ULocale("fa"));
try {
double value = parser.parse("-0,5").doubleValue();
assertEquals("Expect -0.5", -0.5, value);
} catch (ParseException e) {
TestFmwk.errln("Parsing -0.5 should have succeeded.");
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestJB5358.
@Test
public void TestJB5358() {
int numThreads = 10;
String numstr = "12345";
double expected = 12345;
DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
DecimalFormat fmt = new DecimalFormat("#.#", sym);
ArrayList errors = new ArrayList();
ParseThreadJB5358[] threads = new ParseThreadJB5358[numThreads];
for (int i = 0; i < numThreads; i++) {
threads[i] = new ParseThreadJB5358((DecimalFormat) fmt.clone(), numstr, expected, errors);
threads[i].start();
}
for (int i = 0; i < numThreads; i++) {
try {
threads[i].join();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
if (errors.size() != 0) {
StringBuffer errBuf = new StringBuffer();
for (int i = 0; i < errors.size(); i++) {
errBuf.append((String) errors.get(i));
errBuf.append("\n");
}
errln("FAIL: " + errBuf);
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestMissingFieldPositionsCurrency.
@Test
public void TestMissingFieldPositionsCurrency() {
DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance(ULocale.US);
Number number = new Double(92314587.66);
String result = "$92,314,587.66";
checkFormatWithField("currency", formatter, number, result, NumberFormat.Field.CURRENCY, 0, 1);
checkFormatWithField("integer", formatter, number, result, NumberFormat.Field.INTEGER, 1, 11);
checkFormatWithField("grouping separator", formatter, number, result, NumberFormat.Field.GROUPING_SEPARATOR, 3, 4);
checkFormatWithField("decimal separator", formatter, number, result, NumberFormat.Field.DECIMAL_SEPARATOR, 11, 12);
checkFormatWithField("fraction", formatter, number, result, NumberFormat.Field.FRACTION, 12, 14);
}
Aggregations