use of android.icu.text.NumberFormat in project j2objc by google.
the class NumberFormatRegressionTest method Test4408066.
/**
* Test getIntegerInstance();
*/
@Test
public void Test4408066() {
NumberFormat nf1 = NumberFormat.getIntegerInstance();
NumberFormat nf2 = NumberFormat.getIntegerInstance(Locale.CHINA);
// test isParseIntegerOnly
if (!nf1.isParseIntegerOnly() || !nf2.isParseIntegerOnly()) {
errln("Failed : Integer Number Format Instance should set setParseIntegerOnly(true)");
}
// Test format
{
double[] data = { -3.75, -2.5, -1.5, -1.25, 0, 1.0, 1.25, 1.5, 2.5, 3.75, 10.0, 255.5 };
String[] expected = { "-4", "-2", "-2", "-1", "0", "1", "1", "2", "2", "4", "10", "256" };
for (int i = 0; i < data.length; ++i) {
String result = nf1.format(data[i]);
if (!result.equals(expected[i])) {
errln("Failed => Source: " + Double.toString(data[i]) + ";Formatted : " + result + ";but expectted: " + expected[i]);
}
}
}
// Test parse, Parsing should stop at "."
{
String[] data = { "-3.75", "-2.5", "-1.5", "-1.25", "0", "1.0", "1.25", "1.5", "2.5", "3.75", "10.0", "255.5" };
long[] expected = { -3, -2, -1, -1, 0, 1, 1, 1, 2, 3, 10, 255 };
for (int i = 0; i < data.length; ++i) {
Number n = null;
try {
n = nf1.parse(data[i]);
} catch (ParseException e) {
errln("Failed: " + e.getMessage());
}
if (!(n instanceof Long) || (n instanceof Integer)) {
errln("Failed: Integer Number Format should parse string to Long/Integer");
}
if (n.longValue() != expected[i]) {
errln("Failed=> Source: " + data[i] + ";result : " + n.toString() + ";expected :" + Long.toString(expected[i]));
}
}
}
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class NumberFormatRegressionTest method TestSurrogatesParsing.
@Test
public void TestSurrogatesParsing() {
// Test parsing of numbers that use digits from the supplemental planes.
final String[] data = { //
"1\ud801\udca2,3\ud801\udca45.67", //
"\ud801\udca1\ud801\udca2,\ud801\udca3\ud801\udca4\ud801\udca5.\ud801\udca6\ud801\udca7\ud801\udca8", "\ud835\udfd2.\ud835\udfd7E-\ud835\udfd1", "\ud835\udfd3.8E-0\ud835\udfd0" };
final double[] expected = { 12345.67, 12345.678, 0.0049, 0.058 };
NumberFormat nfmt = NumberFormat.getInstance();
for (int i = 0; i < data.length; i++) {
try {
Number n = nfmt.parse(data[i]);
if (expected[i] != n.doubleValue()) {
errln("Failed: Parsed result for " + data[i] + ": " + n.doubleValue() + " / expected: " + expected[i]);
}
} catch (ParseException pe) {
errln("Failed: ParseException is thrown for " + data[i]);
}
}
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class NumberFormatTest method TestCompatibleCurrencies.
@Test
public void TestCompatibleCurrencies() {
NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.US);
// Yen half-width
expectParseCurrency(fmt, Currency.getInstance(Locale.JAPAN), "\u00A51,235");
// Yen full-wdith
expectParseCurrency(fmt, Currency.getInstance(Locale.JAPAN), "\uFFE51,235");
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class NumberFormatTest method TestCurrencyPatterns.
@Test
public void TestCurrencyPatterns() {
int i;
Locale[] locs = NumberFormat.getAvailableLocales();
for (i = 0; i < locs.length; ++i) {
NumberFormat nf = NumberFormat.getCurrencyInstance(locs[i]);
// Make sure currency formats do not have a variable number
// of fraction digits
int min = nf.getMinimumFractionDigits();
int max = nf.getMaximumFractionDigits();
if (min != max) {
String a = nf.format(1.0);
String b = nf.format(1.125);
errln("FAIL: " + locs[i] + " min fraction digits != max fraction digits; " + "x 1.0 => " + a + "; x 1.125 => " + b);
}
// Make sure EURO currency formats have exactly 2 fraction digits
if (nf instanceof DecimalFormat) {
Currency curr = ((DecimalFormat) nf).getCurrency();
if (curr != null && "EUR".equals(curr.getCurrencyCode())) {
if (min != 2 || max != 2) {
String a = nf.format(1.0);
errln("FAIL: " + locs[i] + " is a EURO format but it does not have 2 fraction digits; " + "x 1.0 => " + a);
}
}
}
}
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class NumberFormatTest method TestExplicitParents.
@Test
public void TestExplicitParents() {
// We use these for testing because decimal and grouping separators will be inherited from es_419
// starting with CLDR 2.0
String[] DATA = { "es", "CO", "", "1.250,75", "es", "ES", "", "1.250,75", "es", "GQ", "", "1.250,75", "es", "MX", "", "1,250.75", "es", "US", "", "1,250.75", "es", "VE", "", "1.250,75" };
for (int i = 0; i < DATA.length; i += 4) {
Locale locale = new Locale(DATA[i], DATA[i + 1], DATA[i + 2]);
NumberFormat fmt = NumberFormat.getInstance(locale);
String s = fmt.format(1250.75);
if (s.equals(DATA[i + 3])) {
logln("Ok: 1250.75 x " + locale + " => " + s);
} else {
errln("FAIL: 1250.75 x " + locale + " => " + s + ", expected " + DATA[i + 3]);
}
}
}
Aggregations