## Examples of BigDecimal

• com.ibm.icu.math.BigDecimal
hursley.ibm.com/decimal
[This URL will change.]

### Operator methods

Operations on BigDecimal numbers are controlled by a {@link MathContext} object, which provides the context (precision andother information) for the operation. Methods that can take a MathContext parameter implement the standard arithmetic operators for BigDecimal objects and are known as operator methods. The default settings provided by the constant {@link MathContext#DEFAULT} (digits=9,form=SCIENTIFIC, lostDigits=false, roundingMode=ROUND_HALF_UP) perform general-purpose floating point arithmetic to nine digits of precision. The MathContext parameter must not be null.

Each operator method also has a version provided which does not take a MathContext parameter. For this version of each method, the context settings used are digits=0, form=PLAIN, lostDigits=false, roundingMode=ROUND_HALF_UP; these settings perform fixed point arithmetic with unlimited precision, as defined for the original BigDecimal class in Java 1.1 and Java 1.2.

For monadic operators, only the optional MathContext parameter is present; the operation acts upon the current object.

For dyadic operators, a BigDecimal parameter is always present; it must not be null. The operation acts with the current object being the left-hand operand and the BigDecimal parameter being the right-hand operand.

For example, adding two BigDecimal objects referred to by the names award and extra could be written as any of:

(where acontext is a MathContext object), which would return a BigDecimal object whose value is the result of adding award and extra under the appropriate context settings.

When a BigDecimal operator method is used, a set of rules define what the result will be (and, by implication, how the result would be represented as a character string). These rules are defined in the BigDecimal arithmetic documentation (see the URL above), but in summary:

• Results are normally calculated with up to some maximum number of significant digits. For example, if the MathContext parameter for an operation were MathContext.DEFAULT then the result would be rounded to 9 digits; the division of 2 by 3 would then result in 0.666666667.
You can change the default of 9 significant digits by providing the method with a suitable MathContext object. This lets you calculate using as many digits as you need -- thousands, if necessary. Fixed point (scaled) arithmetic is indicated by using a digits setting of 0 (or omitting the MathContext parameter).
Similarly, you can change the algorithm used for rounding from the default "classic" algorithm.
• In standard arithmetic (that is, when the form setting is not PLAIN), a zero result is always expressed as the single digit '0' (that is, with no sign, decimal point, or exponent part).
• Except for the division and power operators in standard arithmetic, trailing zeros are preserved (this is in contrast to binary floating point operations and most electronic calculators, which lose the information about trailing zeros in the fractional part of results).
So, for example:

new BigDecimal("2.40").add( new BigDecimal("2")) => "4.40"
new BigDecimal("2.40").subtract(new BigDecimal("2")) => "0.40"
new BigDecimal("2.40").multiply(new BigDecimal("2")) => "4.80"
new BigDecimal("2.40").divide( new BigDecimal("2"), def) => "1.2"

where the value on the right of the => would be the result of the operation, expressed as a String, and def (in this and following examples) refers to MathContext.DEFAULT). This preservation of trailing zeros is desirable for most calculations (including financial calculations). If necessary, trailing zeros may be easily removed using division by 1.

• In standard arithmetic, exponential form is used for a result depending on its value and the current setting of digits (the default is 9 digits). If the number of places needed before the decimal point exceeds the digits setting, or the absolute value of the number is less than 0.000001, then the number will be expressed in exponential notation; thus

new BigDecimal("1e+6").multiply(new BigDecimal("1e+6"), def)

results in 1E+12 instead of 1000000000000, and

new BigDecimal("1").divide(new BigDecimal("3E+10"), def)

results in 3.33333333E-11 instead of 0.0000000000333333333.

The form of the exponential notation (scientific or engineering) is determined by the form setting.

The names of methods in this class follow the conventions established by java.lang.Number, java.math.BigInteger, and java.math.BigDecimal in Java 1.1 and Java 1.2. @see MathContext @author Mike Cowlishaw @stable ICU 2.0

• 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

### Examples of com.ibm.icu.math.BigDecimal

 37383940414243444546474849505152535455565758596061626364 public static Map calcTax(DispatchContext dctx, Map context) {         Map result = new HashMap();         List items = (List) context.get("itemProductList");         List amnts = (List) context.get("itemAmountList");         List ishpn = (List) context.get("itemShippingList");         BigDecimal shipping = (BigDecimal) context.get("orderShippingAmount");         GenericValue address = (GenericValue) context.get("shippingAddress");         if (items.size() != amnts.size()) {             result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);             result.put(ModelService.ERROR_MESSAGE, "ERROR: Items, Amount, or ItemShipping lists are not valid size.");             return result;         }         try {             TaxwareUTL utl = new TaxwareUTL();             utl.setShipping(shipping != null ? shipping : BigDecimal.ZERO);             utl.setShipAddress(address);             for (int i = 0; i < items.size(); i++) {                 GenericValue p = (GenericValue) items.get(i);                 BigDecimal amount = (BigDecimal) amnts.get(i);                 BigDecimal ishp = ishpn != null ? (BigDecimal) ishpn.get(i) : BigDecimal.ZERO;                 utl.addItem(p, amount, ishp);             }             int resp = utl.process();
View Full Code Here

### Examples of java.math.BigDecimal

 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

### Examples of java.math.BigDecimal

 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

### Examples of java.math.BigDecimal

 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

### Examples of java.math.BigDecimal

View Full Code Here

### Examples of java.math.BigDecimal

 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

### Examples of java.math.BigDecimal

 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

### Examples of java.math.BigDecimal

 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

### Examples of java.math.BigDecimal

 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

### Examples of java.math.BigDecimal

 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