Search in sources :

Example 91 with BigDecimal

use of java.math.BigDecimal in project j2objc by google.

the class BigDecimalTest method testMathContextConstruction.

public void testMathContextConstruction() {
    String a = "-12380945E+61";
    BigDecimal aNumber = new BigDecimal(a);
    int precision = 6;
    RoundingMode rm = RoundingMode.HALF_DOWN;
    MathContext mcIntRm = new MathContext(precision, rm);
    MathContext mcStr = new MathContext("precision=6 roundingMode=HALF_DOWN");
    MathContext mcInt = new MathContext(precision);
    BigDecimal res = aNumber.abs(mcInt);
    assertEquals("MathContext Constructer with int precision failed", res, new BigDecimal("1.23809E+68"));
    assertEquals("Equal MathContexts are not Equal ", mcIntRm, mcStr);
    assertEquals("Different MathContext are reported as Equal ", mcInt.equals(mcStr), false);
    assertEquals("Equal MathContexts have different hashcodes ", mcIntRm.hashCode(), mcStr.hashCode());
    assertEquals("MathContext.toString() returning incorrect value", mcIntRm.toString(), "precision=6 roundingMode=HALF_DOWN");
}
Also used : RoundingMode(java.math.RoundingMode) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 92 with BigDecimal

use of java.math.BigDecimal in project j2objc by google.

the class DecimalFormat method parse.

/**
     * Parses text from a string to produce a <code>Number</code>.
     * <p>
     * The method attempts to parse text starting at the index given by
     * <code>pos</code>.
     * If parsing succeeds, then the index of <code>pos</code> is updated
     * to the index after the last character used (parsing does not necessarily
     * use all characters up to the end of the string), and the parsed
     * number is returned. The updated <code>pos</code> can be used to
     * indicate the starting point for the next call to this method.
     * If an error occurs, then the index of <code>pos</code> is not
     * changed, the error index of <code>pos</code> is set to the index of
     * the character where the error occurred, and null is returned.
     * <p>
     * The subclass returned depends on the value of {@link #isParseBigDecimal}
     * as well as on the string being parsed.
     * <ul>
     *   <li>If <code>isParseBigDecimal()</code> is false (the default),
     *       most integer values are returned as <code>Long</code>
     *       objects, no matter how they are written: <code>"17"</code> and
     *       <code>"17.000"</code> both parse to <code>Long(17)</code>.
     *       Values that cannot fit into a <code>Long</code> are returned as
     *       <code>Double</code>s. This includes values with a fractional part,
     *       infinite values, <code>NaN</code>, and the value -0.0.
     *       <code>DecimalFormat</code> does <em>not</em> decide whether to
     *       return a <code>Double</code> or a <code>Long</code> based on the
     *       presence of a decimal separator in the source string. Doing so
     *       would prevent integers that overflow the mantissa of a double,
     *       such as <code>"-9,223,372,036,854,775,808.00"</code>, from being
     *       parsed accurately.
     *       <p>
     *       Callers may use the <code>Number</code> methods
     *       <code>doubleValue</code>, <code>longValue</code>, etc., to obtain
     *       the type they want.
     *   <li>If <code>isParseBigDecimal()</code> is true, values are returned
     *       as <code>BigDecimal</code> objects. The values are the ones
     *       constructed by {@link java.math.BigDecimal#BigDecimal(String)}
     *       for corresponding strings in locale-independent format. The
     *       special cases negative and positive infinity and NaN are returned
     *       as <code>Double</code> instances holding the values of the
     *       corresponding <code>Double</code> constants.
     * </ul>
     * <p>
     * <code>DecimalFormat</code> parses all Unicode characters that represent
     * decimal digits, as defined by <code>Character.digit()</code>. In
     * addition, <code>DecimalFormat</code> also recognizes as digits the ten
     * consecutive characters starting with the localized zero digit defined in
     * the <code>DecimalFormatSymbols</code> object.
     *
     * @param text the string to be parsed
     * @param pos  A <code>ParsePosition</code> object with index and error
     *             index information as described above.
     * @return     the parsed value, or <code>null</code> if the parse fails
     * @exception  NullPointerException if <code>text</code> or
     *             <code>pos</code> is null.
     */
@Override
public Number parse(String text, ParsePosition pos) {
    // special case NaN
    if (text.regionMatches(pos.index, symbols.getNaN(), 0, symbols.getNaN().length())) {
        pos.index = pos.index + symbols.getNaN().length();
        return new Double(Double.NaN);
    }
    boolean[] status = new boolean[STATUS_LENGTH];
    if (!subparse(text, pos, positivePrefix, negativePrefix, digitList, false, status)) {
        return null;
    }
    // special case INFINITY
    if (status[STATUS_INFINITE]) {
        if (status[STATUS_POSITIVE] == (multiplier >= 0)) {
            return new Double(Double.POSITIVE_INFINITY);
        } else {
            return new Double(Double.NEGATIVE_INFINITY);
        }
    }
    if (multiplier == 0) {
        if (digitList.isZero()) {
            return new Double(Double.NaN);
        } else if (status[STATUS_POSITIVE]) {
            return new Double(Double.POSITIVE_INFINITY);
        } else {
            return new Double(Double.NEGATIVE_INFINITY);
        }
    }
    if (isParseBigDecimal()) {
        BigDecimal bigDecimalResult = digitList.getBigDecimal();
        if (multiplier != 1) {
            try {
                bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier());
            } catch (ArithmeticException e) {
                // non-terminating decimal expansion
                bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier(), roundingMode);
            }
        }
        if (!status[STATUS_POSITIVE]) {
            bigDecimalResult = bigDecimalResult.negate();
        }
        return bigDecimalResult;
    } else {
        boolean gotDouble = true;
        boolean gotLongMinimum = false;
        double doubleResult = 0.0;
        long longResult = 0;
        // Finally, have DigitList parse the digits into a value.
        if (digitList.fitsIntoLong(status[STATUS_POSITIVE], isParseIntegerOnly())) {
            gotDouble = false;
            longResult = digitList.getLong();
            if (longResult < 0) {
                // got Long.MIN_VALUE
                gotLongMinimum = true;
            }
        } else {
            doubleResult = digitList.getDouble();
        }
        // unneeded conversions between double and long.
        if (multiplier != 1) {
            if (gotDouble) {
                doubleResult /= multiplier;
            } else {
                // Avoid converting to double if we can
                if (longResult % multiplier == 0) {
                    longResult /= multiplier;
                } else {
                    doubleResult = ((double) longResult) / multiplier;
                    gotDouble = true;
                }
            }
        }
        if (!status[STATUS_POSITIVE] && !gotLongMinimum) {
            doubleResult = -doubleResult;
            longResult = -longResult;
        }
        // (bug 4162852).
        if (multiplier != 1 && gotDouble) {
            longResult = (long) doubleResult;
            gotDouble = ((doubleResult != (double) longResult) || (doubleResult == 0.0 && 1 / doubleResult < 0.0)) && !isParseIntegerOnly();
        }
        return gotDouble ? (Number) new Double(doubleResult) : (Number) new Long(longResult);
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) BigDecimal(java.math.BigDecimal)

Example 93 with BigDecimal

use of java.math.BigDecimal in project j2objc by google.

the class BigDecimalTest method test_valueOfJI.

/**
	 * @tests java.math.BigDecimal#valueOf(long, int)
	 */
public void test_valueOfJI() {
    BigDecimal valueOfJI = BigDecimal.valueOf(9223372036854775806L, 5);
    assertTrue("the bigDecimal equivalent of 92233720368547.75806 is wrong", valueOfJI.unscaledValue().toString().equals("9223372036854775806") && valueOfJI.scale() == 5);
    assertTrue("the toString representation of 9223372036854775806 is wrong", valueOfJI.toString().equals("92233720368547.75806"));
    valueOfJI = BigDecimal.valueOf(1234L, 8);
    assertTrue("the bigDecimal equivalent of 92233720368547.75806 is wrong", valueOfJI.unscaledValue().toString().equals("1234") && valueOfJI.scale() == 8);
    assertTrue("the toString representation of 9223372036854775806 is wrong", valueOfJI.toString().equals("0.00001234"));
    valueOfJI = BigDecimal.valueOf(0, 3);
    assertTrue("the bigDecimal equivalent of 92233720368547.75806 is wrong", valueOfJI.unscaledValue().toString().equals("0") && valueOfJI.scale() == 3);
    assertTrue("the toString representation of 9223372036854775806 is wrong", valueOfJI.toString().equals("0.000"));
}
Also used : BigDecimal(java.math.BigDecimal)

Example 94 with BigDecimal

use of java.math.BigDecimal in project j2objc by google.

the class BigDecimalTest method test_ConstructorLjava_math_BigInteger.

/**
	 * @tests java.math.BigDecimal#BigDecimal(java.math.BigInteger)
	 */
public void test_ConstructorLjava_math_BigInteger() {
    BigDecimal big = new BigDecimal(value);
    assertTrue("the BigDecimal value is not initialized properly", big.unscaledValue().equals(value) && big.scale() == 0);
}
Also used : BigDecimal(java.math.BigDecimal)

Example 95 with BigDecimal

use of java.math.BigDecimal in project j2objc by google.

the class BigDecimalTest method test_multiplyLjava_math_BigDecimal.

/**
	 * @tests java.math.BigDecimal#multiply(java.math.BigDecimal)
	 */
public void test_multiplyLjava_math_BigDecimal() {
    BigDecimal multi1 = new BigDecimal(value, 5);
    BigDecimal multi2 = new BigDecimal(2.345D);
    BigDecimal result = multi1.multiply(multi2);
    assertTrue("123.45908 * 2.345 is not correct: " + result, result.toString().startsWith("289.51154260") && result.scale() == multi1.scale() + multi2.scale());
    multi1 = new BigDecimal("34656");
    multi2 = new BigDecimal("-2");
    result = multi1.multiply(multi2);
    assertTrue("34656 * 2 is not correct", result.toString().equals("-69312") && result.scale() == 0);
    multi1 = new BigDecimal(-2.345E-02);
    multi2 = new BigDecimal(-134E130);
    result = multi1.multiply(multi2);
    assertTrue("-2.345E-02 * -134E130 is not correct " + result.doubleValue(), result.doubleValue() == 3.1422999999999997E130 && result.scale() == multi1.scale() + multi2.scale());
    multi1 = new BigDecimal("11235");
    multi2 = new BigDecimal("0");
    result = multi1.multiply(multi2);
    assertTrue("11235 * 0 is not correct", result.doubleValue() == 0 && result.scale() == 0);
    multi1 = new BigDecimal("-0.00234");
    multi2 = new BigDecimal(13.4E10);
    result = multi1.multiply(multi2);
    assertTrue("-0.00234 * 13.4E10 is not correct", result.doubleValue() == -313560000 && result.scale() == multi1.scale() + multi2.scale());
}
Also used : 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