Search in sources :

Example 81 with BigDecimal

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);
}
Also used : Random(java.util.Random) BigDecimal(java.math.BigDecimal) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 82 with BigDecimal

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);
}
Also used : ObjectWithBigDecimal(com.hazelcast.query.SampleObjects.ObjectWithBigDecimal) BigDecimal(java.math.BigDecimal) ObjectWithBigDecimal(com.hazelcast.query.SampleObjects.ObjectWithBigDecimal) QuickTest(com.hazelcast.test.annotation.QuickTest) DateHelperTest(com.hazelcast.query.impl.DateHelperTest) Test(org.junit.Test)

Example 83 with BigDecimal

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))));
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 84 with BigDecimal

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();
}
Also used : BigDecimal(java.math.BigDecimal)

Example 85 with BigDecimal

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());
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Aggregations

BigDecimal (java.math.BigDecimal)5274 Test (org.junit.Test)834 BigInteger (java.math.BigInteger)657 Test (org.testng.annotations.Test)626 LocalDate (org.joda.time.LocalDate)409 ArrayList (java.util.ArrayList)393 ResultSet (java.sql.ResultSet)251 MathContext (java.math.MathContext)220 Timestamp (java.sql.Timestamp)211 PreparedStatement (java.sql.PreparedStatement)207 Date (java.util.Date)175 SQLException (java.sql.SQLException)170 HashMap (java.util.HashMap)157 UUID (java.util.UUID)149 Invoice (org.killbill.billing.invoice.api.Invoice)148 List (java.util.List)136 DateTime (org.joda.time.DateTime)132 RoundingMode (java.math.RoundingMode)129 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)104 Session (org.hibernate.Session)96