## 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

 `123124125126127128129130131` ```        helpTestTransform(new BigInteger("0"), Boolean.FALSE); //\$NON-NLS-1\$         helpTestTransform(new BigInteger("12"), Boolean.TRUE); //\$NON-NLS-1\$     }         @Test public void testBigDecimal2Boolean() throws TransformationException {         helpTestTransform(new BigDecimal("1"), Boolean.TRUE); //\$NON-NLS-1\$         helpTestTransform(new BigDecimal("0"), Boolean.FALSE); //\$NON-NLS-1\$         helpTestTransform(new BigDecimal("0.00"), Boolean.FALSE); //\$NON-NLS-1\$     } ```
View Full Code Here

 `235236237238239240241` ```    @Test public void testStringToLongWithWS() throws Exception {       helpTestTransform(" 1 ", Long.valueOf(1)); //\$NON-NLS-1\$     }         @Test public void testEngineeringNotationFloatToBigInteger() throws Exception {       helpTestTransform(Float.MIN_VALUE, new BigDecimal(Float.MIN_VALUE).toBigInteger());     } ```
View Full Code Here

 `234235236237238239240241242243244245246247248249250251252` ```        // Set grouping elements to null         node.setGroupingElements(null);                 CommandContext context = new CommandContext("pid", "test", null, null, 1);               //\$NON-NLS-1\$ //\$NON-NLS-2\$                 List[] data = new List[] {             Arrays.asList(new Object[] { new BigDecimal("0.0") }),     //\$NON-NLS-1\$             Arrays.asList(new Object[] { new BigDecimal("1.0") }),     //\$NON-NLS-1\$             Arrays.asList(new Object[] { new BigDecimal("2.0") }),     //\$NON-NLS-1\$             Arrays.asList(new Object[] { new BigDecimal("3.0") }),     //\$NON-NLS-1\$             Arrays.asList(new Object[] { new BigDecimal("4.0") }) //\$NON-NLS-1\$         };                 List[] expected = new List[] {             Arrays.asList(new Object[] { new BigDecimal("10.0"), new BigDecimal("2.000000000") }) //\$NON-NLS-1\$ //\$NON-NLS-2\$         };                         List symbols = new ArrayList();         symbols.add(bigDecimal);         FakeTupleSource dataSource = new FakeTupleSource(symbols, data);            ```
View Full Code Here

 `274275276277278279280281282283284285286287288289290291292293` ```        groupingElements.add(col1);         node.setGroupingElements(groupingElements);                 CommandContext context = new CommandContext("pid", "test", null, null, 1);               //\$NON-NLS-1\$ //\$NON-NLS-2\$                 List[] data = new List[] {             Arrays.asList(new Object[] { new Integer(1), new BigDecimal("0.0") }),     //\$NON-NLS-1\$             Arrays.asList(new Object[] { new Integer(1), new BigDecimal("1.0") }),     //\$NON-NLS-1\$             Arrays.asList(new Object[] { new Integer(2), new BigDecimal("2.0") }),     //\$NON-NLS-1\$             Arrays.asList(new Object[] { new Integer(2), new BigDecimal("3.0") }),     //\$NON-NLS-1\$             Arrays.asList(new Object[] { new Integer(2), new BigDecimal("4.0") }) //\$NON-NLS-1\$         };                 List[] expected = new List[] {             Arrays.asList(new Object[] { new Integer(1), new BigDecimal("1.0"), new BigDecimal("0.500000000") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$             Arrays.asList(new Object[] { new Integer(2), new BigDecimal("9.0"), new BigDecimal("3.000000000") }) //\$NON-NLS-1\$ //\$NON-NLS-2\$         };                         List symbols = new ArrayList();         symbols.add(col1);         symbols.add(bigDecimal); ```
View Full Code Here

 `9899100101102103104105106107108` ```    public void testMostTypes() throws Exception {         Object[][] results = new Object[][] {             new Object[] { new Integer(0), "ABCDEFGHIJ", new Float(0), new Long(0), new Double(0), new Byte((byte)0), //\$NON-NLS-1\$                 LoopbackExecution.SQL_DATE_VAL, LoopbackExecution.TIME_VAL,                 LoopbackExecution.TIMESTAMP_VAL, Boolean.FALSE,                 new BigInteger("0"), new BigDecimal("0"), "ABCDEFGHIJ", //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                 new Short((short)0), new Character('c')                  }          };                 helpTestQuery("SELECT intkey, StringKey, floatnum, longnum, doublenum, bytenum, " + //\$NON-NLS-1\$ ```
View Full Code Here

 `152215231524152515261527152815291530153115321533153415351536153715381539` ```                    break;                 case Types.DECIMAL:                 case Types.NUMERIC:                     BigDecimal num = rs.getBigDecimal(columnNumber);                     if (num == null) {                         break;                     }                     if (num.scale() > 0) {                         newprop.setFloatValue(num.doubleValue());                     } else {                         newprop.setIntegerValue(num.longValue());                     }                     break;                 case Types.VARBINARY: ```
View Full Code Here

 `274275276277278279280` ```    this.chararray_field = (boolean_field ? new char[] {char_field} : null);     this.bytearray_field = (boolean_field ? new byte[] {byte_field} : null);     this.doublearray_field = (boolean_field ? new double[] {double_field} : null);     this.string_field = (boolean_field ? "str" + longval : null);     biginteger_field = (boolean_field ? new BigInteger("" + longval) : null);     bigdecimal_field = (boolean_field ? new BigDecimal(biginteger_field) : null);   } ```
View Full Code Here

 `329330331332333334335336337338339340` ```      Assert.assertEquals("bytearray_field bad value", 1, bytearray_field.length);       Assert.assertEquals("bytearray_field bad value", byte_field, bytearray_field[0]);       Assert.assertEquals("doubleArray_field bad value", 1, doublearray_field.length);       Assert.assertEquals("string_field bad value", "str" + longval, string_field);       Assert.assertEquals("biginteger_field bad value", new BigInteger("" + longval), biginteger_field);       BigDecimal expected = new BigDecimal(biginteger_field);       BigDecimal found = bigdecimal_field;       Assert.assertTrue("bigdecimal_field bad value, expected " + expected         + ", found: " + found,         expected.compareTo(found)==0);     }   } ```
View Full Code Here

 `135136137138139140141` ```    this.chararray_field = (boolean_field ? new char[] {char_field} : null);     this.bytearray_field = (boolean_field ? new byte[] {byte_field} : null);     this.doubleArray_field = (boolean_field ? new double[] {double_field} : null);     this.string_field = (boolean_field ? "str" + longval : null);     biginteger_field = (boolean_field ? new BigInteger("" + longval) : null);     bigdecimal_field = (boolean_field ? new BigDecimal(biginteger_field) : null);   } ```
View Full Code Here

 `190191192193194195196197198199200201` ```      Assert.assertEquals("bytearray_field bad value", 1, bytearray_field.length);       Assert.assertEquals("bytearray_field bad value", byte_field, bytearray_field[0]);       Assert.assertEquals("doubleArray_field bad value", 1, doubleArray_field.length);       Assert.assertEquals("string_field bad value", "str" + longval, string_field);       Assert.assertEquals("biginteger_field bad value", new BigInteger("" + longval), biginteger_field);       BigDecimal expected = new BigDecimal(biginteger_field);       BigDecimal found = bigdecimal_field;       Assert.assertTrue("bigdecimal_field bad value, expected " + expected         + ", found: " + found,         expected.compareTo(found)==0);     }   } ```
View Full Code Here