use of java.math.RoundingMode in project Tundra by Permafrost.
the class decimal method multiply.
public static final void multiply(IData pipeline) throws ServiceException {
// --- <<IS-START(multiply)>> ---
// @subtype unknown
// @sigtype java 3.5
// [i] record:0:optional $operands
// [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 {
IData operands = IDataHelper.get(cursor, "$operands", IData.class);
int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
// support $decimals and $decimal input for backwards-compatibility
boolean backwardsCompatiblityRequired = false;
if (operands == null) {
String[] list = IDataHelper.get(cursor, "$decimals", String[].class);
String decimal = IDataHelper.get(cursor, "$decimal", String.class);
IDataMap map = new IDataMap();
if (list != null)
map.put("$decimals", list);
if (decimal != null)
map.put("$decimal", decimal);
operands = map;
backwardsCompatiblityRequired = true;
}
BigDecimal[] decimals = BigDecimalHelper.normalize(IDataHelper.getLeafValues(operands));
BigDecimal result = BigDecimalHelper.round(BigDecimalHelper.multiply(decimals), precision, rounding);
if (backwardsCompatiblityRequired) {
IDataHelper.put(cursor, "$decimal", result, String.class, false);
} else {
IDataHelper.put(cursor, "$result", result, String.class, false);
}
} finally {
cursor.destroy();
}
// --- <<IS-END>> ---
}
use of java.math.RoundingMode in project Tundra by Permafrost.
the class decimal method subtract.
public static final void subtract(IData pipeline) throws ServiceException {
// --- <<IS-START(subtract)>> ---
// @subtype unknown
// @sigtype java 3.5
// [i] field:0:optional $minuend
// [i] field:0:optional $subtrahend
// [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 minuend = IDataHelper.get(cursor, "$minuend", BigDecimal.class);
BigDecimal subtrahend = IDataHelper.get(cursor, "$subtrahend", BigDecimal.class);
int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
// support $decimal.x and $decimal.y inputs for backwards-compatibility
boolean backwardsCompatiblityRequired = false;
if (minuend == null && subtrahend == null) {
minuend = IDataHelper.get(cursor, "$decimal.x", BigDecimal.class);
subtrahend = IDataHelper.get(cursor, "$decimal.y", BigDecimal.class);
backwardsCompatiblityRequired = true;
}
BigDecimal result = BigDecimalHelper.round(BigDecimalHelper.subtract(minuend, subtrahend), precision, rounding);
if (backwardsCompatiblityRequired) {
IDataHelper.put(cursor, "$decimal", result, String.class, false);
} else {
IDataHelper.put(cursor, "$result", result, String.class, false);
}
} finally {
cursor.destroy();
}
// --- <<IS-END>> ---
}
use of java.math.RoundingMode in project Tundra by Permafrost.
the class decimal method minimum.
public static final void minimum(IData pipeline) throws ServiceException {
// --- <<IS-START(minimum)>> ---
// @subtype unknown
// @sigtype java 3.5
// [i] record:0:optional $operands
// [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 $minimum
IDataCursor cursor = pipeline.getCursor();
try {
IData operands = IDataHelper.get(cursor, "$operands", IData.class);
int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
// support $decimals input for backwards-compatibility
boolean backwardsCompatiblityRequired = false;
if (operands == null) {
String[] list = IDataHelper.get(cursor, "$decimals", String[].class);
IDataMap map = new IDataMap();
if (list != null)
map.put("$decimals", list);
operands = map;
backwardsCompatiblityRequired = true;
}
BigDecimal[] decimals = BigDecimalHelper.normalize(IDataHelper.getLeafValues(operands));
BigDecimal result = BigDecimalHelper.round(BigDecimalHelper.minimum(decimals), precision, rounding);
if (backwardsCompatiblityRequired) {
IDataHelper.put(cursor, "$decimal", result, String.class, false);
} else {
IDataHelper.put(cursor, "$minimum", result, String.class, false);
}
} finally {
cursor.destroy();
}
// --- <<IS-END>> ---
}
use of java.math.RoundingMode in project Tundra by Permafrost.
the class decimal method maximum.
public static final void maximum(IData pipeline) throws ServiceException {
// --- <<IS-START(maximum)>> ---
// @subtype unknown
// @sigtype java 3.5
// [i] record:0:optional $operands
// [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 $maximum
IDataCursor cursor = pipeline.getCursor();
try {
IData operands = IDataHelper.get(cursor, "$operands", IData.class);
int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
// support $decimals input for backwards-compatibility
boolean backwardsCompatiblityRequired = false;
if (operands == null) {
String[] list = IDataHelper.get(cursor, "$decimals", String[].class);
IDataMap map = new IDataMap();
if (list != null)
map.put("$decimals", list);
operands = map;
backwardsCompatiblityRequired = true;
}
BigDecimal[] decimals = BigDecimalHelper.normalize(IDataHelper.getLeafValues(operands));
BigDecimal result = BigDecimalHelper.round(BigDecimalHelper.maximum(decimals), precision, rounding);
if (backwardsCompatiblityRequired) {
IDataHelper.put(cursor, "$decimal", result, String.class, false);
} else {
IDataHelper.put(cursor, "$maximum", result, String.class, false);
}
} 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
}
}
Aggregations