use of java.math.RoundingMode in project robovm by robovm.
the class OldBigDecimalConstructorsTest method testConstrCharIntIntMathContextException1.
/**
* new BigDecimal(char[] value, int offset, int len, MathContext mc);
*/
public void testConstrCharIntIntMathContextException1() {
char[] value = { '-', '1', '2', '3', '8', '0', '.', '4', '7', '3', '8', 'E', '-', '4', '2', '3' };
int offset = 3;
int len = 120;
int precision = 4;
RoundingMode rm = RoundingMode.CEILING;
MathContext mc = new MathContext(precision, rm);
try {
new BigDecimal(value, offset, len, mc);
fail("NumberFormatException has not been thrown");
} catch (NumberFormatException e) {
}
}
use of java.math.RoundingMode in project robovm by robovm.
the class OldBigDecimalConstructorsTest method testConstrStringMathContext.
/**
* new BigDecimal(String value, MathContext)
*/
public void testConstrStringMathContext() {
String a = "-238768787678287e214";
int precision = 5;
RoundingMode rm = RoundingMode.CEILING;
MathContext mc = new MathContext(precision, rm);
String res = "-23876";
int resScale = -224;
BigDecimal result = new BigDecimal(a, mc);
assertEquals("incorrect value", res, result.unscaledValue().toString());
assertEquals("incorrect scale", resScale, result.scale());
// Now test more than just RoundingMode.CEILING:
//
String biStr = new String("12345678901234567890123456789012345.0E+10");
String nbiStr = new String("-12345678901234567890123456789012345.E+10");
BigDecimal bd;
mc = new MathContext(31, RoundingMode.UP);
bd = new BigDecimal(biStr, mc);
assertEquals("incorrect value", "1.234567890123456789012345678902E+44", bd.toString());
bd = new BigDecimal(nbiStr, mc);
assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString());
mc = new MathContext(28, RoundingMode.DOWN);
bd = new BigDecimal(biStr, mc);
assertEquals("incorrect value", "1.234567890123456789012345678E+44", bd.toString());
bd = new BigDecimal(nbiStr, mc);
assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString());
mc = new MathContext(33, RoundingMode.CEILING);
bd = new BigDecimal(biStr, mc);
assertEquals("incorrect value", "1.23456789012345678901234567890124E+44", bd.toString());
bd = new BigDecimal(nbiStr, mc);
assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString());
mc = new MathContext(34, RoundingMode.UNNECESSARY);
try {
bd = new BigDecimal(biStr, mc);
fail("No ArithmeticException for RoundingMode.UNNECESSARY");
} catch (ArithmeticException e) {
// expected
}
try {
bd = new BigDecimal(nbiStr, mc);
fail("No ArithmeticException for RoundingMode.UNNECESSARY");
} catch (ArithmeticException e) {
// expected
}
mc = new MathContext(7, RoundingMode.FLOOR);
bd = new BigDecimal("1000000.9", mc);
assertEquals("incorrect value", "1000000", bd.toString());
}
use of java.math.RoundingMode in project robovm by robovm.
the class OldBigDecimalArithmeticTest method test_DivideBigDecimalRoundingExc.
/**
* java.math.BigDecimal#divide(java.math.BigDecimal,
* java.math.RoundingMode) divide(BigDecimal, RoundingMode)
*/
public void test_DivideBigDecimalRoundingExc() {
String a = "3736186567876876578956958765675671119238118911893939591735";
String b = "74723342238476237823787879183470";
RoundingMode rm = RoundingMode.UNNECESSARY;
BigDecimal aNumber = new BigDecimal(new BigInteger(a));
BigDecimal bNumber = new BigDecimal(new BigInteger(b));
try {
aNumber.divide(bNumber, rm);
fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY divider");
} catch (java.lang.ArithmeticException ae) {
// expected
}
try {
bNumber = new BigDecimal(0);
aNumber.divide(bNumber, rm);
fail("ArithmeticException is not thrown for zero divider");
} catch (java.lang.ArithmeticException ae) {
// expected
}
}
use of java.math.RoundingMode in project robovm by robovm.
the class OldBigDecimalArithmeticTest method test_DivideBigDecimalRoundingModeCEILING.
/**
* java.math.BigDecimal#divide(java.math.BigDecimal,
* java.math.RoundingMode) divide(BigDecimal, RoundingMode)
*/
public void test_DivideBigDecimalRoundingModeCEILING() {
String a = "3736186567876876578956958765675671119238118911893939591735";
String b = "74723342238476237823787879183470";
RoundingMode rm = RoundingMode.CEILING;
String c = "50000260373164286401361914";
BigDecimal aNumber = new BigDecimal(new BigInteger(a));
BigDecimal bNumber = new BigDecimal(new BigInteger(b));
BigDecimal result = aNumber.divide(bNumber, rm);
assertEquals("incorrect value", c, result.toString());
}
use of java.math.RoundingMode in project robovm by robovm.
the class BigDecimalArithmeticTest method testDivideBigDecimalScaleRoundingModeHALF_DOWN.
/**
* divide(BigDecimal, scale, RoundingMode)
*/
public void testDivideBigDecimalScaleRoundingModeHALF_DOWN() {
String a = "3736186567876876578956958765675671119238118911893939591735";
int aScale = 5;
String b = "74723342238476237823787879183470";
int bScale = 15;
int newScale = 7;
RoundingMode rm = RoundingMode.HALF_DOWN;
String c = "500002603731642864013619132621009722.1803810";
BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
BigDecimal result = aNumber.divide(bNumber, newScale, rm);
assertEquals("incorrect value", c, result.toString());
assertEquals("incorrect scale", newScale, result.scale());
}
Aggregations