use of java.math.BigDecimal in project hazelcast by hazelcast.
the class MapMaxAggregationTest method testBigDecimalMax.
@Test
public void testBigDecimalMax() throws Exception {
BigDecimal[] values = buildPlainValues(new ValueProvider<BigDecimal>() {
@Override
public BigDecimal provideRandom(Random random) {
return BigDecimal.valueOf(10000.0D + random(1000, 2000));
}
}, BigDecimal.class);
BigDecimal expectation = BigDecimal.ZERO;
for (int i = 0; i < values.length; i++) {
BigDecimal value = values[i];
expectation = i == 0 ? value : expectation.max(value);
}
Aggregation<String, BigDecimal, BigDecimal> aggregation = Aggregations.bigDecimalMax();
BigDecimal result = testMax(values, aggregation);
assertEquals(expectation, result);
}
use of java.math.BigDecimal in project hazelcast by hazelcast.
the class SqlPredicateTest method testSql_withBigDecimal.
@Test
public void testSql_withBigDecimal() {
ObjectWithBigDecimal value = new ObjectWithBigDecimal(new BigDecimal("1.23E3"));
assertSqlMatching("attribute > '" + new BigDecimal("1.23E2") + "'", value);
assertSqlMatching("attribute >= '" + new BigDecimal("1.23E3") + "'", value);
assertSqlNotMatching("attribute = '" + new BigDecimal("1.23") + "'", value);
assertSqlMatching("attribute = '1.23E3'", value);
assertSqlMatching("attribute = 1.23E3", value);
assertSqlNotMatching("attribute = 1.23", value);
}
use of java.math.BigDecimal in project hazelcast by hazelcast.
the class TypeConverterTest method testBigIntegerConvert_whenPassedBigDecimalValue_thenConvertToBigInteger.
@Test
public void testBigIntegerConvert_whenPassedBigDecimalValue_thenConvertToBigInteger() throws Exception {
BigDecimal value = BigDecimal.valueOf(4.9999);
Comparable expectedBigIntValue = BigInteger.valueOf(value.longValue());
Comparable comparable = TypeConverters.BIG_INTEGER_CONVERTER.convert(value);
assertThat(comparable, allOf(is(instanceOf(BigInteger.class)), is(equalTo(expectedBigIntValue))));
}
use of java.math.BigDecimal in project guava by hceylan.
the class BigIntegerMath method divide.
/**
* Returns the result of dividing {@code p} by {@code q}, rounding using the specified
* {@code RoundingMode}.
*
* @throws ArithmeticException if {@code q == 0}, or if {@code mode == UNNECESSARY} and {@code a}
* is not an integer multiple of {@code b}
*/
public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode) {
BigDecimal pDec = new BigDecimal(p);
BigDecimal qDec = new BigDecimal(q);
return pDec.divide(qDec, 0, mode).toBigIntegerExact();
}
use of java.math.BigDecimal in project guava by google.
the class LongMathTest method computeMeanSafely.
/**
* Computes the mean in a way that is obvious and resilient to
* overflow by using BigInteger arithmetic.
*/
private static long computeMeanSafely(long x, long 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 Long.parseLong(bigMean.toString());
}
Aggregations