Examples of java.math.BigDecimal

• java.math.BigDecimal
ger=19, scale=2}
but
{@code 21/110 = 0.190 // integer=190, scale=3}

Note that for add, subtract, and multiply, the reduction in scale will equal the number of digit positions of the exact result which are discarded. If the rounding causes a carry propagation to create a new high-order digit position, an additional digit of the result is discarded than when no new digit position is created.

Other methods may have slightly different rounding semantics. For example, the result of the {@code pow} method using the{@linkplain #pow(int,MathContext) specified algorithm} canoccasionally differ from the rounded mathematical result by more than one unit in the last place, one {@linkplain #ulp() ulp}.

Two types of operations are provided for manipulating the scale of a {@code BigDecimal}: scaling/rounding operations and decimal point motion operations. Scaling/rounding operations ( {@link #setScale setScale} and {@link #round round}) return a {@code BigDecimal} whose value is approximately (or exactly) equalto that of the operand, but whose scale or precision is the specified value; that is, they increase or decrease the precision of the stored number with minimal effect on its value. Decimal point motion operations ( {@link #movePointLeft movePointLeft} and{@link #movePointRight movePointRight}) return a {@code BigDecimal} created from the operand by moving the decimalpoint a specified distance in the specified direction.

For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of {@code BigDecimal} methods. Thepseudo-code expression {@code (i + j)} is shorthand for "a{@code BigDecimal} whose value is that of the {@code BigDecimal}{@code i} added to that of the {@code BigDecimal}{@code j}." The pseudo-code expression {@code (i == j)} isshorthand for " {@code true} if and only if the{@code BigDecimal} {@code i} represents the same value as the{@code BigDecimal} {@code j}." Other pseudo-code expressions are interpreted similarly. Square brackets are used to represent the particular {@code BigInteger} and scale pair defining a{@code BigDecimal} value; for example [19, 2] is the{@code BigDecimal} numerically equal to 0.19 having a scale of 2.

Note: care should be exercised if {@code BigDecimal} objectsare used as keys in a {@link java.util.SortedMap SortedMap} orelements in a {@link java.util.SortedSet SortedSet} since{@code BigDecimal}'s natural ordering is inconsistent with equals. See {@link Comparable}, {@link java.util.SortedMap} or {@link java.util.SortedSet} for moreinformation.

All methods and constructors for this class throw {@code NullPointerException} when passed a {@code null} objectreference for any input parameter. @see BigInteger @see MathContext @see RoundingMode @see java.util.SortedMap @see java.util.SortedSet @author Josh Bloch @author Mike Cowlishaw @author Joseph D. Darcy

 `686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725` ```        stat.execute("INSERT INTO TEST VALUES(7, -99999999.99, -99999999.99)");         stat.execute("INSERT INTO TEST VALUES(8, NULL, NULL)");         rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");         assertResultSetMeta(rs, 3, new String[] { "ID", "D", "R" },                 new int[] { Types.INTEGER, Types.DOUBLE, Types.REAL }, new int[] { 10, 17, 7 }, new int[] { 0, 0, 0 });         BigDecimal bd;         rs.next();         assertTrue(rs.getInt(1) == 1);         assertTrue(!rs.wasNull());         assertTrue(rs.getInt(2) == -1);         assertTrue(rs.getInt(3) == -1);         assertTrue(!rs.wasNull());         bd = rs.getBigDecimal(2);         assertTrue(bd.compareTo(new BigDecimal("-1.00")) == 0);         assertTrue(!rs.wasNull());         o = rs.getObject(2);         trace(o.getClass().getName());         assertTrue(o instanceof Double);         assertTrue(((Double) o).compareTo(new Double("-1.00")) == 0);         o = rs.getObject(3);         trace(o.getClass().getName());         assertTrue(o instanceof Float);         assertTrue(((Float) o).compareTo(new Float("-1.00")) == 0);         rs.next();         assertTrue(rs.getInt(1) == 2);         assertTrue(!rs.wasNull());         assertTrue(rs.getInt(2) == 0);         assertTrue(!rs.wasNull());         assertTrue(rs.getInt(3) == 0);         assertTrue(!rs.wasNull());         bd = rs.getBigDecimal(2);         assertTrue(bd.compareTo(new BigDecimal("0.00")) == 0);         assertTrue(!rs.wasNull());         bd = rs.getBigDecimal(3);         assertTrue(bd.compareTo(new BigDecimal("0.00")) == 0);         assertTrue(!rs.wasNull());         rs.next();         assertEquals(1.0, rs.getDouble(2));         assertEquals(1.0f, rs.getFloat(3));         rs.next(); ```
View Full Code Here

 `11111112111311141115111611171118111911201121112211231124112511261127` ```            assertEquals(got, expected);         }     }     private void checkColumnBigDecimal(ResultSet rs, int column, int i, String bd) throws SQLException {         BigDecimal bd1 = rs.getBigDecimal(column);         int i1 = rs.getInt(column);         if (bd == null) {             trace("should be: null");             assertTrue(rs.wasNull());         } else {             trace("BigDecimal i=" + i + " bd=" + bd + " ; i1=" + i1 + " bd1=" + bd1);             assertTrue(!rs.wasNull());             assertTrue(i1 == i);             assertTrue(bd1.compareTo(new BigDecimal(bd)) == 0);         }     } ```
View Full Code Here

 `613614615616617618619620621622623` ```                tuples[index].add(null);                 tuples[index].add(null);                 tuples[index].add(null);                 tuples[index].add(new Short((short)i));                 tuples[index].add(new BigInteger(i+"")); //\$NON-NLS-1\$                 tuples[index].add(new BigDecimal(i+".0")); //\$NON-NLS-1\$                 tuples[index].add(null);             }                         dataMgr.registerTuples(metadata, "bqt1.smalla", tuples); //\$NON-NLS-1\$             ```
View Full Code Here

 `48554856485748584859486048614862486348644865` ```        // Create query         String sql = "SELECT AVG(IntKey), AVG(IntNum), AVG(FloatNum), AVG(LongNum), AVG(DoubleNum), AVG(ByteNum), AVG(ShortValue), AVG(BigIntegerValue), AVG(BigDecimalValue) FROM BQT1.SmallA"; //\$NON-NLS-1\$                 // Create expected results         List[] expected = new List[] {             Arrays.asList(new Object[] { new Double(1.5), new Double(1.5), new Double(1.5), new Double(1.5), new Double(1.5), new Double(1.5), new Double(1.5), new BigDecimal("1.500000000"), new BigDecimal("1.500000000") }),  //\$NON-NLS-1\$//\$NON-NLS-2\$         };                // Construct data manager with data         FakeDataManager dataManager = new FakeDataManager();         sampleDataBQT_defect11682(dataManager); ```
View Full Code Here

 `63506351635263536354635563566357635863596360` ```                tuples[row].add(String.valueOf(row)); //StringNum                 for(int col=0; col<10; col++) { //FloatNum, LongNum, DoubleNum, ByteNum, DateValue, TimeValue, TimestampValue, BooleanValue, CharValue, ShortValue                     tuples[row].add(null);                    }                    tuples[row].add(new BigInteger(String.valueOf(row))); //BigIntegerValue                 tuples[row].add(new BigDecimal(row)); //BigDecimalValue                 tuples[row].add(null);    //ObjectValue             }             dataMgr.registerTuples(metadata, groupName, tuples);         }     }    ```
View Full Code Here

 `174175176177178179180181182183184185186187188189` ```   * @return Hash code, based on value    */   public int hashCode() {     if(this.value != null) {       if (this.value instanceof BigDecimal) {         BigDecimal bd = (BigDecimal)this.value;         int xsign = bd.signum();             if (xsign == 0)                 return 0;             bd = bd.stripTrailingZeros();             return bd.hashCode();       }       return this.value.hashCode();     }     return 0;   } ```
View Full Code Here

 `523524525526527528529530` ```        /**      * Big decimal comparisons should ignore precision.      */     @Test public void testBigDecimalEquality() throws Exception {       CompareCriteria crit = new CompareCriteria(new Constant(new BigDecimal("3.10")), CompareCriteria.EQ, new Constant(new BigDecimal("3.1"))); //\$NON-NLS-1\$ //\$NON-NLS-2\$       assertTrue(Evaluator.evaluate(crit));     } ```
View Full Code Here

 `402403404405406407408` ```        helpTestCommandPayload(Boolean.TRUE, "BOGUS", null); //\$NON-NLS-1\$     }         @Test public void testBigDecimalFromDoubleDivision() throws Exception {       Expression ex = TestFunctionResolving.getExpression("convert(1.0, bigdecimal)/3");       assertEquals(new BigDecimal("0.3333333333333333"), Evaluator.evaluate(ex));     } ```
View Full Code Here

 `407408409410411412413` ```      assertEquals(new BigDecimal("0.3333333333333333"), Evaluator.evaluate(ex));     }         @Test public void testBigDecimalDivision() throws Exception {       Expression ex = TestFunctionResolving.getExpression("1/convert('3.0', bigdecimal)");       assertEquals(new BigDecimal("0.3333333333333333"), Evaluator.evaluate(ex));     } ```
View Full Code Here

 `10671068106910701071107210731074107510761077` ```    private Object[] helpGetStoredProcDefaultValues() {                 // This needs to match what's in FakeMetadataFactory.example1 for this stored proc         return new Object[]  {             new Constant("x"), //\$NON-NLS-1\$             new Constant(new BigDecimal("13.0")),//\$NON-NLS-1\$             new Constant(new BigInteger("13")),//\$NON-NLS-1\$             new Constant(Boolean.TRUE),             new Constant(new Byte("1")),//\$NON-NLS-1\$             new Constant(new Character('q')),             new Constant(Date.valueOf("2003-03-20")),//\$NON-NLS-1\$ ```
View Full Code Here