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

 `223224225226227228229230231232233234235236237238239` ```        // Create query          String sql = "select c.id as CustomerID, First, Last, a.account_id as AccountID, transaction_id as TransactionID, txn_type AS TxnCode, Amount, source from (CustomerMaster.Customers c join CustomerMaster.Locations l on c.id=l.id) join Accounts.Accounts a on c.id=a.customer_id and l.location=a.source where c.first='Miles' order by accountid option makenotdep c, l"; //\$NON-NLS-1\$                  // Create expected results         List[] expected = new List[] {                          Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(540), new Long(1002), new Integer(1), new BigDecimal("7.20"), "EU" }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$ //\$NON-NLS-4\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(540), new Long(1003), new Integer(2), new BigDecimal("1000.00"), "EU" }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$ //\$NON-NLS-4\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(123), new Integer(1), new BigDecimal("100.00"), "US" }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$ //\$NON-NLS-4\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(127), new Integer(2), new BigDecimal("250.00"), "US" }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$ //\$NON-NLS-4\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(128), new Integer(3), new BigDecimal("1000.00"), "US" }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$ //\$NON-NLS-4\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15001), new Long(134), new Integer(1), new BigDecimal("10.00"), "US" }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$ //\$NON-NLS-4\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15001), new Long(201), new Integer(1), new BigDecimal("10.00"), "US" }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$ //\$NON-NLS-4\$                 };             // Construct data manager with data         FakeMetadataFacade metadata = exampleVirtualDepJoin();                FakeDataManager dataManager = new FakeDataManager(); ```
View Full Code Here

 `290291292293294295296297298299300301302303304305306` ```        // Create query          String sql = "SELECT * from Master.Transactions where last = 'Davis'"; //\$NON-NLS-1\$                  // Create expected results         List[] expected = new List[] {                          Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(123), new Integer(1), new BigDecimal("100.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(127), new Integer(2), new BigDecimal("250.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(128), new Integer(3), new BigDecimal("1000.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15001), new Long(134), new Integer(1), new BigDecimal("10.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15001), new Long(201), new Integer(1), new BigDecimal("10.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(540), new Long(1002), new Integer(1), new BigDecimal("7.20") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(540), new Long(1003), new Integer(2), new BigDecimal("1000.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                 };             // Construct data manager with data         FakeMetadataFacade metadata = exampleVirtualDepJoin();                FakeDataManager dataManager = new FakeDataManager(); ```
View Full Code Here

 `345346347348349350351352353354355` ```        // Create query          String sql = "select first, last, sum(amount) from Europe.CustAccts e join CustomerMaster.Customers c on c.id=e.id where c.first='Miles' group by c.id, first, last"; //\$NON-NLS-1\$                  // Create expected results         List[] expected = new List[] {                          Arrays.asList(new Object[] { "Miles", "Davis", new BigDecimal("1007.20") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                 };             // Construct data manager with data         FakeMetadataFacade metadata = exampleVirtualDepJoin();                FakeDataManager dataManager = new FakeDataManager(); ```
View Full Code Here

 `421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465` ```                 List expected = new LinkedList();                         // Create expected results         expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(123), new Integer(1), new BigDecimal("100.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         if (!unique) {             expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(123), new Integer(1), new BigDecimal("100.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         }                    expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(127), new Integer(2), new BigDecimal("250.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         if (!unique) {             expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(127), new Integer(2), new BigDecimal("250.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         }         expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(128), new Integer(3), new BigDecimal("1000.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         if (!unique) {             expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(128), new Integer(3), new BigDecimal("1000.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         }         expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15001), new Long(134), new Integer(1), new BigDecimal("10.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         if (!unique) {             expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15001), new Long(134), new Integer(1), new BigDecimal("10.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         }         expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15001), new Long(201), new Integer(1), new BigDecimal("10.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         if (!unique) {             expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15001), new Long(201), new Integer(1), new BigDecimal("10.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         }         expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(540), new Long(1002), new Integer(1), new BigDecimal("7.20") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         if (!unique) {             expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(540), new Long(1002), new Integer(1), new BigDecimal("7.20") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         }         expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(540), new Long(1003), new Integer(2), new BigDecimal("1000.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         if (!unique) {             expected.add(Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(540), new Long(1003), new Integer(2), new BigDecimal("1000.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         }         expected.add(Arrays.asList(new Object[] { new Long(200), "CloneA", "Davis", new Long(16000), new Long(207), new Integer(3), new BigDecimal("12.34") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         expected.add(Arrays.asList(new Object[] { new Long(200), "CloneA", "Davis", new Long(16000), new Long(299), new Integer(3), new BigDecimal("950.34") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         expected.add(Arrays.asList(new Object[] { new Long(200), "CloneA", "Davis", new Long(550), new Long(1004), new Integer(3), new BigDecimal("542.20") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         expected.add(Arrays.asList(new Object[] { new Long(200), "CloneA", "Davis", new Long(550), new Long(1005), new Integer(1), new BigDecimal("99.99") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         expected.add(Arrays.asList(new Object[] { new Long(300), "CloneB", "Davis", new Long(620), new Long(1006), new Integer(1), new BigDecimal("10000.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         expected.add(Arrays.asList(new Object[] { new Long(300), "CloneB", "Davis", new Long(620), new Long(1007), new Integer(2), new BigDecimal("0.75") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         expected.add(Arrays.asList(new Object[] { new Long(300), "CloneB", "Davis", new Long(630), new Long(1008), new Integer(2), new BigDecimal("62.00") })); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$         // Construct data manager with data         FakeMetadataFacade metadata = exampleVirtualDepJoin();                FakeDataManager dataManager = new FakeDataManager();         sampleDataVirtualDepJoin(dataManager, metadata); ```
View Full Code Here

 `503504505506507508509510511512513514515516517518519` ```        // Create query          String sql = "SELECT * from Master.Transactions where last = 'Davis'"; //\$NON-NLS-1\$                  // Create expected results         List[] expected = new List[] {                          Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(123), new Integer(1), new BigDecimal("100.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(127), new Integer(2), new BigDecimal("250.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15000), new Long(128), new Integer(3), new BigDecimal("1000.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15001), new Long(134), new Integer(1), new BigDecimal("10.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(15001), new Long(201), new Integer(1), new BigDecimal("10.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(540), new Long(1002), new Integer(1), new BigDecimal("7.20") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                         Arrays.asList(new Object[] { new Long(100), "Miles", "Davis", new Long(540), new Long(1003), new Integer(2), new BigDecimal("1000.00") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                 };             // Construct data manager with data         FakeMetadataFacade metadata = exampleVirtualDepJoin();                FakeDataManager dataManager = new FakeDataManager(); ```
View Full Code Here

 `564565566567568569570571572573574575576577578579580` ```        dataMgr.registerTuples(             groupID,             elementSymbols,                         new List[] {                 Arrays.asList(new Object[] { new Long(100), new Long(5401002), new Short((short)1), new BigDecimal("7.20") }), //\$NON-NLS-1\$                 Arrays.asList(new Object[] { new Long(100), new Long(5401003), new Short((short)2), new BigDecimal("1000.00") }), //\$NON-NLS-1\$                 Arrays.asList(new Object[] { new Long(200), new Long(5501004), new Short((short)3), new BigDecimal("542.20") }), //\$NON-NLS-1\$                 Arrays.asList(new Object[] { new Long(200), new Long(5501005), new Short((short)1), new BigDecimal("99.99") }), //\$NON-NLS-1\$                 Arrays.asList(new Object[] { new Long(300), new Long(6201006), new Short((short)1), new BigDecimal("10000.00") }), //\$NON-NLS-1\$                 Arrays.asList(new Object[] { new Long(300), new Long(6201007), new Short((short)2), new BigDecimal("0.75") }), //\$NON-NLS-1\$                 Arrays.asList(new Object[] { new Long(300), new Long(6301008), new Short((short)2), new BigDecimal("62.00") }), //\$NON-NLS-1\$                 } );            // Group CustomerMaster.Customers         groupID = (FakeMetadataObject) metadata.getGroupID("CustomerMaster.Customers"); //\$NON-NLS-1\$         elementIDs = metadata.getElementIDsInGroupID(groupID); ```
View Full Code Here

 `680681682683684685686687688689690` ```        // Create query          String sql = "select first, last, sum(amount) from Europe.CustAccts e makenotdep join CustomerMaster.Customers c on c.id=e.id where c.first='Miles' group by c.id, first, last"; //\$NON-NLS-1\$                  // Create expected results         List[] expected = new List[] {                          Arrays.asList(new Object[] { "Miles", "Davis", new BigDecimal("1007.20") }), //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                 };             // Construct data manager with data         FakeMetadataFacade metadata = exampleVirtualDepJoin();                FakeDataManager dataManager = new FakeDataManager(); ```
View Full Code Here

 `216217218219220221222223224225226` ```                    String[] list = new String[values.size()];                     values.toArray(list);                     return new Arg(String[].class, list);                 } else if (readIf("BigDecimal")) {                     read("(");                     BigDecimal value = new BigDecimal((String) parseValue().getValue());                     read(")");                     return new Arg(BigDecimal.class, value);                 } else {                     throw new RuntimeException("Unsupported constructor: " + readToken());                 } ```
View Full Code Here

 `8586878889909192939495` ```            case DOUBLE:                 return new Double( ((Double)sum).doubleValue() / count );             case BIG_DECIMAL:                 try {                     return ((BigDecimal)sum).divide(new BigDecimal(count), AVG_SCALE, BigDecimal.ROUND_HALF_UP);                 } catch(ArithmeticException e) {                     throw new FunctionExecutionException(e, "ERR.015.001.0048", QueryPlugin.Util.getString("ERR.015.001.0048", sum, new Integer(count))); //\$NON-NLS-1\$ //\$NON-NLS-2\$                 }             default:                 throw new AssertionError("unknown accumulator type"); //\$NON-NLS-1\$ ```
View Full Code Here

 `979899100101102103104105106107108109110111112113114115116117118119120121122123124125` ```                    break;                 case BIG_INTEGER:                     this.sum = new BigInteger(String.valueOf(0));                     break;                 case BIG_DECIMAL:                     this.sum = new BigDecimal(0);                     break;             }         }                     switch(this.accumulatorType) {                    case LONG:                 this.sum = new Long(((Long)this.sum).longValue() + ((Number)input).longValue());                 break;             case DOUBLE:                 this.sum = new Double(((Double)this.sum).doubleValue() + ((Number)input).doubleValue());                 break;             case BIG_INTEGER:                 this.sum = ((BigInteger)this.sum).add( (BigInteger) input );                 break;             case BIG_DECIMAL:                 if (input instanceof BigInteger) {                     BigInteger bigIntegerInput = (BigInteger) input;                     this.sum = ((BigDecimal)this.sum).add( new BigDecimal(bigIntegerInput) );                 } else {                     this.sum = ((BigDecimal)this.sum).add( (BigDecimal) input );                 }                 break;            } ```
View Full Code Here