use of java.math.RoundingMode in project voltdb by VoltDB.
the class TestDecimalRoundingSuite method testDecimalScaleInsertion.
public void testDecimalScaleInsertion() throws Exception {
Boolean roundIsEnabled = Boolean.valueOf(m_defaultRoundingEnablement);
RoundingMode roundMode = RoundingMode.valueOf(m_defaultRoundingMode);
assert (m_config instanceof LocalCluster);
LocalCluster localCluster = (LocalCluster) m_config;
Map<String, String> props = localCluster.getAdditionalProcessEnv();
if (props != null) {
roundIsEnabled = Boolean.valueOf(props.containsKey(m_roundingEnabledProperty) ? props.get(m_roundingEnabledProperty) : "true");
roundMode = RoundingMode.valueOf(props.containsKey(m_roundingModeProperty) ? props.get(m_roundingModeProperty) : "HALF_UP");
System.out.printf("Rounding is %senabled, mode is %s\n", roundIsEnabled ? "" : "not ", roundMode.toString());
} else {
System.out.printf("Default rounding (%s), Default Rounding mode (%s).\n", roundIsEnabled.toString(), roundMode.toString());
}
doTestDecimalScaleInsertion(roundIsEnabled, roundMode);
}
use of java.math.RoundingMode in project Tundra by Permafrost.
the class measure method convert.
// ---( server methods )---
public static final void convert(IData pipeline) throws ServiceException {
// --- <<IS-START(convert)>> ---
// @subtype unknown
// @sigtype java 3.5
// [i] field:0:optional $value
// [i] field:0:required $unit.input {"t","kg","g","lb","oz","km","m","cm","mm","mi","yd","ft","in"}
// [i] field:0:required $unit.output {"t","kg","g","lb","oz","km","m","cm","mm","mi","yd","ft","in"}
// [i] field:0:optional $precision
// [i] field:0:optional $rounding {"HALF_UP","CEILING","DOWN","FLOOR","HALF_DOWN","HALF_EVEN","UNNECESSARY","UP"}
// [o] field:0:optional $result
IDataCursor cursor = pipeline.getCursor();
try {
BigDecimal value = IDataHelper.get(cursor, "$value", BigDecimal.class);
String inputUnit = IDataHelper.get(cursor, "$unit.input", String.class);
String outputUnit = IDataHelper.get(cursor, "$unit.output", String.class);
int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
if (value != null) {
IDataHelper.put(cursor, "$result", BigDecimalHelper.round(MeasureHelper.convert(value, UnitHelper.parse(inputUnit), UnitHelper.parse(outputUnit), MathContext.DECIMAL128), precision, rounding), String.class);
}
} finally {
cursor.destroy();
}
// --- <<IS-END>> ---
}
use of java.math.RoundingMode in project robovm by robovm.
the class OldBigDecimalConstructorsTest method testConstrLongMathContext.
/**
* new BigDecimal(long, MathContext)
*/
public void testConstrLongMathContext() {
long a = 4576578677732546982L;
int precision = 5;
RoundingMode rm = RoundingMode.CEILING;
MathContext mc = new MathContext(precision, rm);
String res = "45766";
int resScale = -14;
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
//
BigDecimal bd;
mc = new MathContext(15, RoundingMode.UP);
bd = new BigDecimal(78901234567890125L, mc);
assertEquals("incorrect value", "7.89012345678902E+16", bd.toString());
bd = new BigDecimal(-78901234567890125L, mc);
assertEquals("incorrect value", "-7.89012345678902E+16", bd.toString());
mc = new MathContext(12, RoundingMode.DOWN);
bd = new BigDecimal(78901234567890125L, mc);
assertEquals("incorrect value", "7.89012345678E+16", bd.toString());
bd = new BigDecimal(-78901234567890125L, mc);
assertEquals("incorrect value", "-7.89012345678E+16", bd.toString());
mc = new MathContext(15, RoundingMode.CEILING);
bd = new BigDecimal(78901234567890125L, mc);
assertEquals("incorrect value", "7.89012345678902E+16", bd.toString());
bd = new BigDecimal(-78901234567890125L, mc);
assertEquals("incorrect value", "-7.89012345678901E+16", bd.toString());
mc = new MathContext(12, RoundingMode.FLOOR);
bd = new BigDecimal(78901234567890125L, mc);
assertEquals("incorrect value", "7.89012345678E+16", bd.toString());
bd = new BigDecimal(-78901234567890125L, mc);
assertEquals("incorrect value", "-7.89012345679E+16", bd.toString());
mc = new MathContext(16, RoundingMode.HALF_EVEN);
bd = new BigDecimal(78901234567890125L, mc);
assertEquals("incorrect value", "7.890123456789012E+16", bd.toString());
bd = new BigDecimal(-78901234567890125L, mc);
assertEquals("incorrect value", "-7.890123456789012E+16", bd.toString());
bd = new BigDecimal(-78901234567890135L, mc);
assertEquals("incorrect value", "-7.890123456789014E+16", bd.toString());
mc = new MathContext(16, RoundingMode.HALF_UP);
bd = new BigDecimal(78901234567890125L, mc);
assertEquals("incorrect value", "7.890123456789013E+16", bd.toString());
bd = new BigDecimal(-78901234567890125L, mc);
assertEquals("incorrect value", "-7.890123456789013E+16", bd.toString());
mc = new MathContext(16, RoundingMode.HALF_DOWN);
bd = new BigDecimal(78901234567890125L, mc);
assertEquals("incorrect value", "7.890123456789012E+16", bd.toString());
bd = new BigDecimal(-78901234567890125L, mc);
assertEquals("incorrect value", "-7.890123456789012E+16", bd.toString());
mc = new MathContext(8, RoundingMode.UNNECESSARY);
try {
bd = new BigDecimal(78901234567890125L, mc);
fail("No ArithmeticException for RoundingMode.UNNECESSARY");
} catch (ArithmeticException e) {
// expected
}
try {
bd = new BigDecimal(-78901234567890125L, mc);
fail("No ArithmeticException for RoundingMode.UNNECESSARY");
} catch (ArithmeticException e) {
// expected
}
}
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());
}
Aggregations