public static BigDecimal castToDecimal(Object val) { if (val == null) { return null; } if (val instanceof BigDecimal) { return (BigDecimal) val; } if (val instanceof String) { return new BigDecimal((String) val); } if (val instanceof Float) { return new BigDecimal((Float) val); } if (val instanceof Double) { return new BigDecimal((Double) val); } return BigDecimal.valueOf(((Number) val).longValue()); }
@Test public void toInitializedScoreHSS() { assertEquals(scoreDefinitionHSS.createScore(BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)), scoreDefinitionHSS.createScore(BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)).toInitializedScore()); assertEquals(scoreDefinitionHSS.createScore(BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)), scoreDefinitionHSS.createScoreUninitialized(-7, BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)).toInitializedScore()); }
/** * Test for {@link NumberUtils#toScaledBigDecimal(BigDecimal, int, RoundingMode)}. */ @Test public void testToScaledBigDecimalBigDecimalIRM() { assertTrue("toScaledBigDecimal(BigDecimal, int, RoudingMode) 1 failed", NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(123.456), 1, RoundingMode.CEILING).equals(BigDecimal.valueOf(123.5))); assertTrue("toScaledBigDecimal(BigDecimal, int, RoudingMode) 2 failed", NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.5159), 3, RoundingMode.FLOOR).equals(BigDecimal.valueOf(23.515))); assertTrue("toScaledBigDecimal(BigDecimal, int, RoudingMode) 3 failed", NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.525), 2, RoundingMode.HALF_UP).equals(BigDecimal.valueOf(23.53))); assertTrue("toScaledBigDecimal(BigDecimal, int, RoudingMode) 4 failed", NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.521), 4, RoundingMode.HALF_EVEN) .multiply(BigDecimal.valueOf(1000)) .toString() .equals("23521.0000")); assertTrue("toScaledBigDecimal(BigDecimal, int, RoudingMode) 5 failed", NumberUtils.toScaledBigDecimal((BigDecimal) null, 2, RoundingMode.HALF_UP).equals(BigDecimal.ZERO)); }
@Test public void testMathExprBround() throws HiveException { double[] vArr = { 1.5, 2.5, -1.5, -2.5, 1.49, 1.51 }; for (double v : vArr) { double v1 = RoundUtils.bround(v, 0); double v2 = MathExpr.bround(v); Assert.assertEquals(v1, v2, 0.00001); double v3 = BigDecimal.valueOf(v).setScale(0, ROUND_HALF_EVEN).doubleValue(); Assert.assertEquals(v3, v2, 0.00001); } }
/** * @throws Exception If failed. */ @Test public void testDecimal() throws Exception { BigDecimal val; assertEquals((val = BigDecimal.ZERO), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Long.MAX_VALUE, 0)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Long.MIN_VALUE, 0)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Long.MAX_VALUE, 8)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Long.MIN_VALUE, 8)), marshalUnmarshal(val)); assertEquals((val = new BigDecimal(new BigInteger("-79228162514264337593543950336"))), marshalUnmarshal(val)); }
/** * @throws Exception If failed. */ @Test public void testNegativeScaleRoundingModeDecimal() throws Exception { BigDecimal val; assertEquals((val = BigDecimal.ZERO.setScale(-1, RoundingMode.HALF_UP)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Long.MAX_VALUE).setScale(-3, RoundingMode.HALF_DOWN)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Long.MIN_VALUE).setScale(-5, RoundingMode.HALF_EVEN)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Integer.MAX_VALUE).setScale(-8, RoundingMode.UP)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Integer.MIN_VALUE).setScale(-10, RoundingMode.DOWN)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Double.MAX_VALUE).setScale(-12, RoundingMode.CEILING)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Double.MIN_VALUE).setScale(-15, RoundingMode.FLOOR)), marshalUnmarshal(val)); }
@Override public synchronized long getAverageSessionAliveTime() { //this method needs to be synchronised to make sure the session count and the total are in sync if(expiredSessionCount == 0) { return 0; } return new BigDecimal(totalSessionLifetime).divide(BigDecimal.valueOf(expiredSessionCount), MathContext.DECIMAL128).longValue(); }
private PercentType byteToPercentType(int byteValue) { BigDecimal percentValue = new BigDecimal(byteValue).multiply(BigDecimal.valueOf(100)) .divide(BigDecimal.valueOf(255), 2, BigDecimal.ROUND_HALF_UP); return new PercentType(percentValue); }
/** * @since 2.7.3 */ public static BigDecimal toBigDecimal(long seconds, int nanoseconds) { if (nanoseconds == 0L) { // 14-Mar-2015, tatu: Let's retain one zero to avoid interpretation // as integral number if (seconds == 0L) { // except for "0.0" where it can not be done without scientific notation return BigDecimal.ZERO.setScale(1); } return BigDecimal.valueOf(seconds).setScale(9); } return new BigDecimal(toDecimal(seconds, nanoseconds)); }
@Override public HardSoftBigDecimalScore multiply(double multiplicand) { // Intentionally not taken "new BigDecimal(multiplicand, MathContext.UNLIMITED)" // because together with the floor rounding it gives unwanted behaviour BigDecimal multiplicandBigDecimal = BigDecimal.valueOf(multiplicand); // The (unspecified) scale/precision of the multiplicand should have no impact on the returned scale/precision return new HardSoftBigDecimalScore( (int) Math.floor(initScore * multiplicand), hardScore.multiply(multiplicandBigDecimal).setScale(hardScore.scale(), RoundingMode.FLOOR), softScore.multiply(multiplicandBigDecimal).setScale(softScore.scale(), RoundingMode.FLOOR)); }
/** * get the value which is converted to specified unit. * * @param unit size unit * @return the converted value */ public double get(Unit unit) { if (value == 0) { return value; } int diff = this.unit.getOrderOfSize() - unit.getOrderOfSize(); if (diff == 0) { return value; } BigDecimal rval = BigDecimal.valueOf(value); for (int i = 0; i != Math.abs(diff); ++i) { rval = diff > 0 ? rval.multiply(SCALE_BASE) : rval.divide(SCALE_BASE); } return rval.doubleValue(); }
@Test public void withInitScore() { assertEquals(scoreDefinitionHSS.createScoreUninitialized(-7, BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)), scoreDefinitionHSS.createScore(BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)).withInitScore(-7)); }
/** * Test for {@link NumberUtils#toScaledBigDecimal(BigDecimal)}. */ @Test public void testToScaledBigDecimalBigDecimal() { assertTrue("toScaledBigDecimal(BigDecimal) 1 failed", NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(123.456)).equals(BigDecimal.valueOf(123.46))); // Test RoudingMode.HALF_EVEN default rounding. assertTrue("toScaledBigDecimal(BigDecimal) 2 failed", NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.515)).equals(BigDecimal.valueOf(23.52))); assertTrue("toScaledBigDecimal(BigDecimal) 3 failed", NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.525)).equals(BigDecimal.valueOf(23.52))); assertTrue("toScaledBigDecimal(BigDecimal) 4 failed", NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.525)) .multiply(BigDecimal.valueOf(100)).toString() .equals("2352.00")); assertTrue("toScaledBigDecimal(BigDecimal) 5 failed", NumberUtils.toScaledBigDecimal((BigDecimal) null).equals(BigDecimal.ZERO)); }
/** * @throws Exception If failed. */ @Test public void testNegativeScaleDecimal() throws Exception { BigDecimal val; assertEquals((val = BigDecimal.valueOf(Long.MAX_VALUE, -1)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Long.MIN_VALUE, -2)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Long.MAX_VALUE, -3)), marshalUnmarshal(val)); assertEquals((val = BigDecimal.valueOf(Long.MIN_VALUE, -4)), marshalUnmarshal(val)); }
public BigDecimal getLimit() { if (limit.equals("false")) { return BigDecimal.valueOf(Double.MAX_VALUE); } else { return new BigDecimal(limit); } }
/** * Computes the mean in a way that is obvious and resilient to overflow by using BigInteger * arithmetic. */ private static int computeMeanSafely(int x, int y) { BigInteger bigX = BigInteger.valueOf(x); BigInteger bigY = BigInteger.valueOf(y); BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR); // parseInt blows up on overflow as opposed to intValue() which does not. return Integer.parseInt(bigMean.toString()); }