use of java.text.DecimalFormat in project j2objc by google.
the class DecimalFormatTest method test_formatDouble_maximumFractionDigits.
public void test_formatDouble_maximumFractionDigits() {
DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.US));
df.setMaximumFractionDigits(3);
assertEquals(3, df.getMaximumFractionDigits());
assertEquals("1.235", df.format(1.23456));
df.setMinimumFractionDigits(4);
assertEquals(4, df.getMaximumFractionDigits());
assertEquals("456.0000", df.format(456));
df = new DecimalFormat("##0.#");
df.setMaximumFractionDigits(30);
assertEquals("0", df.format(0.0));
assertEquals("-0", df.format(-0.0));
assertEquals("1", df.format(1.0));
assertEquals("-1", df.format(-1.0));
}
use of java.text.DecimalFormat in project j2objc by google.
the class DecimalFormatTest method testSerializationHarmonyRICompatible.
public void testSerializationHarmonyRICompatible() throws Exception {
NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
DecimalFormat df = null;
if (!(nf instanceof DecimalFormat)) {
throw new Error("This NumberFormat is not a DecimalFormat");
}
df = (DecimalFormat) nf;
ObjectInputStream oinput = null;
DecimalFormat deserializedDF = null;
try {
oinput = new ObjectInputStream(getClass().getResource("/serialization/org/apache/harmony/tests/java/text/DecimalFormat.ser").openStream());
deserializedDF = (DecimalFormat) oinput.readObject();
} finally {
try {
if (null != oinput) {
oinput.close();
}
} catch (Exception e) {
// ignore
}
}
assertEquals(df.getNegativePrefix(), deserializedDF.getNegativePrefix());
assertEquals(df.getNegativeSuffix(), deserializedDF.getNegativeSuffix());
assertEquals(df.getPositivePrefix(), deserializedDF.getPositivePrefix());
assertEquals(df.getPositiveSuffix(), deserializedDF.getPositiveSuffix());
assertEquals(df.getCurrency(), deserializedDF.getCurrency());
DecimalFormatSymbolsTest.assertDecimalFormatSymbolsRIFrance(deserializedDF.getDecimalFormatSymbols());
assertEquals(df.getGroupingSize(), df.getGroupingSize());
assertEquals(df.getMaximumFractionDigits(), deserializedDF.getMaximumFractionDigits());
assertEquals(df.getMaximumIntegerDigits(), deserializedDF.getMaximumIntegerDigits());
assertEquals(df.getMinimumFractionDigits(), deserializedDF.getMinimumFractionDigits());
assertEquals(df.getMinimumIntegerDigits(), deserializedDF.getMinimumIntegerDigits());
assertEquals(df.getMultiplier(), deserializedDF.getMultiplier());
// Deliberately omitted this assertion. Since different data resource
// will cause the assertion fail.
// assertEquals(df, deserializedDF);
}
use of java.text.DecimalFormat in project j2objc by google.
the class DecimalFormatTest method test_getGroupingSize.
public void test_getGroupingSize() {
DecimalFormat df = new DecimalFormat("###0.##");
assertEquals("Wrong unset size", 0, df.getGroupingSize());
df = new DecimalFormat("#,##0.##");
assertEquals("Wrong set size", 3, df.getGroupingSize());
df = new DecimalFormat("#,###,###0.##");
assertEquals("Wrong multiple set size", 4, df.getGroupingSize());
}
use of java.text.DecimalFormat in project j2objc by google.
the class DecimalFormatTest method test_formatObject.
public void test_formatObject() {
DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
// format maxLong
FieldPosition pos = new FieldPosition(0);
StringBuffer out = format.format(new Long(Long.MAX_VALUE), new StringBuffer(), pos);
assertTrue("Wrong result L1: " + out, out.toString().equals("9,223,372,036,854,775,807"));
// format minLong
pos = new FieldPosition(0);
out = format.format(new Long(Long.MIN_VALUE), new StringBuffer(), pos);
assertTrue("Wrong result L2: " + out, out.toString().equals("-9,223,372,036,854,775,808"));
// format maxLong of type BigInteger
pos = new FieldPosition(0);
out = format.format(new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)), new StringBuffer(), pos);
assertTrue("Wrong result BI1: " + out, out.toString().equals("9,223,372,036,854,775,807"));
// format minLong of type BigInteger
pos = new FieldPosition(0);
out = format.format(new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)), new StringBuffer(), pos);
assertTrue("Wrong result BI2: " + out, out.toString().equals("-9,223,372,036,854,775,808"));
// format maxLong + 1
java.math.BigInteger big;
pos = new FieldPosition(0);
big = new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)).add(new java.math.BigInteger("1"));
out = format.format(big, new StringBuffer(), pos);
assertTrue("Wrong result BI3: " + out, out.toString().equals("9,223,372,036,854,775,808"));
// format minLong - 1
pos = new FieldPosition(0);
big = new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)).add(new java.math.BigInteger("-1"));
out = format.format(big, new StringBuffer(), pos);
assertTrue("Wrong result BI4: " + out, out.toString().equals("-9,223,372,036,854,775,809"));
// format big decimal
pos = new FieldPosition(0);
out = format.format(new java.math.BigDecimal("51.348"), new StringBuffer(), pos);
assertTrue("Wrong result BD1: " + out, out.toString().equals("51.348"));
// format big decimal
pos = new FieldPosition(0);
out = format.format(new java.math.BigDecimal("51"), new StringBuffer(), pos);
assertTrue("Wrong result BD2: " + out, out.toString().equals("51"));
// format big decimal Double.MAX_VALUE * 2
java.math.BigDecimal bigDecimal;
pos = new FieldPosition(0);
final String doubleMax2 = "359,538,626,972,463,141,629,054,847,463,408," + "713,596,141,135,051,689,993,197,834,953,606,314,521,560,057,077," + "521,179,117,265,533,756,343,080,917,907,028,764,928,468,642,653," + "778,928,365,536,935,093,407,075,033,972,099,821,153,102,564,152," + "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806," + "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476," + "354,361,838,599,762,500,808,052,368,249,716,736";
bigDecimal = new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(Double.MAX_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
assertTrue("Wrong result BDmax2: " + out, out.toString().equals(doubleMax2));
// format big decimal Double.MIN_VALUE + Double.MIN_VALUE
// and Double.MIN_VALUE - Double.MIN_VALUE
pos = new FieldPosition(0);
bigDecimal = new BigDecimal(Double.MIN_VALUE).add(new BigDecimal(Double.MIN_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
bigDecimal = new BigDecimal(Float.MAX_VALUE).add(new BigDecimal(Float.MAX_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
final String BDFloatMax2 = "680,564,693,277,057,719,623,408,366,969,033,850,880";
assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals(BDFloatMax2));
// format big decimal Float.MIN_VALUE + Float.MIN_VALUE
// and Float.MIN_VALUE - Float.MIN_VALUE
bigDecimal = new BigDecimal(Float.MIN_VALUE).add(new BigDecimal(Float.MIN_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
final String BDFloatMin2 = "0";
bigDecimal = new BigDecimal(Float.MIN_VALUE).subtract(new BigDecimal(Float.MIN_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals(BDFloatMin2));
}
use of java.text.DecimalFormat in project j2objc by google.
the class DecimalFormatTest method test_setPositivePrefix.
public void test_setPositivePrefix() throws Exception {
DecimalFormat format = new DecimalFormat();
assertEquals("", format.getPositivePrefix());
format.setPositivePrefix("PosPrf");
assertEquals("PosPrf", format.getPositivePrefix());
assertTrue(format.parse("PosPrf123.45").doubleValue() == 123.45);
format.setPositivePrefix("");
assertEquals("", format.getPositivePrefix());
format.setPositivePrefix(null);
assertNull(format.getPositivePrefix());
}
Aggregations