Search in sources :

Example 21 with BigDecimal

use of java.math.BigDecimal in project groovy by apache.

the class ObjectRangeTest method testGet.

public void testGet() {
    Range r = createRange(10, 20);
    for (int i = 0; i < 10; i++) {
        Integer value = (Integer) r.get(i);
        assertEquals("Item at index: " + i, i + 10, value.intValue());
    }
    r = createRange(new BigDecimal("3.2"), new BigDecimal("9.9"));
    for (int i = 0; i < r.size(); i++) {
        BigDecimal value = (BigDecimal) r.get(i);
        assertEquals("Item at index: " + i, new BigDecimal("3.2").add(new BigDecimal("" + i)), value);
    }
    r = new ObjectRange(10, 20, false);
    for (int i = 0; i < 10; i++) {
        Integer value = (Integer) r.get(i);
        assertEquals("Item at index: " + i, i + 10, value.intValue());
    }
    r = new ObjectRange(10, 20, true);
    for (int i = 0; i < 10; i++) {
        Integer value = (Integer) r.get(i);
        assertEquals("Item at index: " + i, 20 - i, value.intValue());
    }
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 22 with BigDecimal

use of java.math.BigDecimal in project groovy by apache.

the class NumberRange method calcSize.

void calcSize(Comparable from, Comparable to, Number stepSize) {
    int tempsize = 0;
    boolean shortcut = false;
    if (isIntegral(stepSize)) {
        if ((from instanceof Integer || from instanceof Long) && (to instanceof Integer || to instanceof Long)) {
            // let's fast calculate the size
            final BigInteger fromNum = new BigInteger(from.toString());
            final BigInteger toTemp = new BigInteger(to.toString());
            final BigInteger toNum = inclusive ? toTemp : toTemp.subtract(BigInteger.ONE);
            final BigInteger sizeNum = new BigDecimal(toNum.subtract(fromNum)).divide(new BigDecimal(stepSize.longValue()), BigDecimal.ROUND_DOWN).toBigInteger().add(BigInteger.ONE);
            tempsize = sizeNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == -1 ? sizeNum.intValue() : Integer.MAX_VALUE;
            shortcut = true;
        } else if (((from instanceof BigDecimal || from instanceof BigInteger) && to instanceof Number) || ((to instanceof BigDecimal || to instanceof BigInteger) && from instanceof Number)) {
            // let's fast calculate the size
            final BigDecimal fromNum = new BigDecimal(from.toString());
            final BigDecimal toTemp = new BigDecimal(to.toString());
            final BigDecimal toNum = inclusive ? toTemp : toTemp.subtract(new BigDecimal("1.0"));
            final BigInteger sizeNum = toNum.subtract(fromNum).divide(new BigDecimal(stepSize.longValue()), BigDecimal.ROUND_DOWN).toBigInteger().add(BigInteger.ONE);
            tempsize = sizeNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == -1 ? sizeNum.intValue() : Integer.MAX_VALUE;
            shortcut = true;
        }
    }
    if (!shortcut) {
        // let's brute-force calculate the size by iterating start to end
        final Iterator iter = new StepIterator(this, stepSize);
        while (iter.hasNext()) {
            tempsize++;
            // integer overflow
            if (tempsize < 0) {
                break;
            }
            iter.next();
        }
        // integer overflow
        if (tempsize < 0) {
            tempsize = Integer.MAX_VALUE;
        }
    }
    size = tempsize;
}
Also used : BigInteger(java.math.BigInteger) Iterator(java.util.Iterator) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 23 with BigDecimal

use of java.math.BigDecimal in project groovy by apache.

the class BigDecimalMath method divideImpl.

public Number divideImpl(Number left, Number right) {
    BigDecimal bigLeft = toBigDecimal(left);
    BigDecimal bigRight = toBigDecimal(right);
    try {
        return bigLeft.divide(bigRight);
    } catch (ArithmeticException e) {
        // set a DEFAULT precision if otherwise non-terminating
        int precision = Math.max(bigLeft.precision(), bigRight.precision()) + DIVISION_EXTRA_PRECISION;
        BigDecimal result = bigLeft.divide(bigRight, new MathContext(precision));
        int scale = Math.max(Math.max(bigLeft.scale(), bigRight.scale()), DIVISION_MIN_SCALE);
        if (result.scale() > scale)
            result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return result;
    }
}
Also used : BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 24 with BigDecimal

use of java.math.BigDecimal in project groovy by apache.

the class Numbers method parseDecimal.

/**
    *  Builds a Number from the given decimal descriptor.  Uses BigDecimal,
    *  unless, Double or Float is requested.
    *
    *  @param  text literal text to parse
    *  @return instantiated Number object
    *  @throws NumberFormatException if the number does not fit within the type
    *          requested by the type specifier suffix (invalid numbers don't make
    *          it here)
    */
public static Number parseDecimal(String text) {
    text = text.replace("_", "");
    int length = text.length();
    //
    // Strip off any type specifier and convert it to lower
    // case, if present.
    char type = 'x';
    if (isNumericTypeSpecifier(text.charAt(length - 1), true)) {
        type = Character.toLowerCase(text.charAt(length - 1));
        text = text.substring(0, length - 1);
        length -= 1;
    }
    //
    // Build the specified type or default to BigDecimal
    BigDecimal value = new BigDecimal(text);
    switch(type) {
        case 'f':
            if (value.compareTo(MAX_FLOAT) <= 0 && value.compareTo(MIN_FLOAT) >= 0) {
                return new Float(text);
            }
            throw new NumberFormatException("out of range");
        case 'd':
            if (value.compareTo(MAX_DOUBLE) <= 0 && value.compareTo(MIN_DOUBLE) >= 0) {
                return new Double(text);
            }
            throw new NumberFormatException("out of range");
        case 'g':
        default:
            return value;
    }
}
Also used : BigDecimal(java.math.BigDecimal)

Example 25 with BigDecimal

use of java.math.BigDecimal in project hadoop by apache.

the class TestSplitters method testBigDecimalSplitter.

@Test(timeout = 2000)
public void testBigDecimalSplitter() throws Exception {
    BigDecimalSplitter splitter = new BigDecimalSplitter();
    ResultSet result = mock(ResultSet.class);
    List<InputSplit> splits = splitter.split(configuration, result, "column");
    assertSplits(new String[] { ".*column IS NULL" }, splits);
    when(result.getString(1)).thenReturn("result1");
    when(result.getString(2)).thenReturn("result2");
    when(result.getBigDecimal(1)).thenReturn(new BigDecimal(10));
    when(result.getBigDecimal(2)).thenReturn(new BigDecimal(12));
    splits = splitter.split(configuration, result, "column1");
    assertSplits(new String[] { "column1 >= 10 column1 < 11", "column1 >= 11 column1 <= 12" }, splits);
}
Also used : ResultSet(java.sql.ResultSet) InputSplit(org.apache.hadoop.mapreduce.InputSplit) DataDrivenDBInputSplit(org.apache.hadoop.mapreduce.lib.db.DataDrivenDBInputFormat.DataDrivenDBInputSplit) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

BigDecimal (java.math.BigDecimal)4643 Test (org.junit.Test)634 BigInteger (java.math.BigInteger)624 Test (org.testng.annotations.Test)573 LocalDate (org.joda.time.LocalDate)409 ArrayList (java.util.ArrayList)351 ResultSet (java.sql.ResultSet)222 Timestamp (java.sql.Timestamp)205 PreparedStatement (java.sql.PreparedStatement)201 SQLException (java.sql.SQLException)159 Invoice (org.killbill.billing.invoice.api.Invoice)148 Date (java.util.Date)146 UUID (java.util.UUID)144 MathContext (java.math.MathContext)140 DateTime (org.joda.time.DateTime)132 HashMap (java.util.HashMap)130 RoundingMode (java.math.RoundingMode)128 List (java.util.List)113 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)104 Session (org.hibernate.Session)96