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

 `5960616263646566676869` ```      }     });     {       Source source = new Source();       source.bigDecimal = new BigDecimal(1);       Destination result = mapper.map(source, Destination.class);       assertThat(result.bigDecimal, equalTo(new Double(1)));     }     { ```
View Full Code Here

 `174175176177178179180181182183184` ```  public static SimpleValue wrap(MetaType type, String value) {     if (type instanceof SimpleMetaType) {       SimpleMetaType st = (SimpleMetaType)type;             if (SimpleMetaType.BIGDECIMAL.equals(st)) {         return new SimpleValueSupport(st, new BigDecimal(value));       } else if (SimpleMetaType.BIGINTEGER.equals(st)) {         return new SimpleValueSupport(st, new BigInteger(value));       } else if (SimpleMetaType.BOOLEAN.equals(st)) {         return new SimpleValueSupport(st, Boolean.valueOf(value));       } else if (SimpleMetaType.BOOLEAN_PRIMITIVE.equals(st)) { ```
View Full Code Here

 `297298299300301302303304305306307308` ```        }     }         private static class BigDecimalColumnSerializer extends ColumnSerializer {         protected void writeObject(ObjectOutput out, Object obj) throws IOException {             BigDecimal val = (BigDecimal)obj;             out.writeInt(val.scale());             BigInteger unscaled = val.unscaledValue();             byte[] bytes = unscaled.toByteArray();             out.writeInt(bytes.length);             out.write(bytes);         } ```
View Full Code Here

 `309310311312313314315` ```        protected Object readObject(ObjectInput in) throws IOException {             int scale = in.readInt();             int length = in.readInt();             byte[] bytes = new byte[length];             in.readFully(bytes);             return new BigDecimal(new BigInteger(bytes), scale);         } ```
View Full Code Here

 `40414243444546474849` ```   * @throws TransformationException if value is an incorrect input type or    * the transformation fails    */   public Object transformDirect(Object value) throws TransformationException {     try {       return new BigDecimal(((String)value).trim());     } catch(NumberFormatException e) {       throw new TransformationException("ERR.003.029.0014", CorePlugin.Util.getString("ERR.003.029.0014", value)); //\$NON-NLS-1\$ //\$NON-NLS-2\$     }   } ```
View Full Code Here

 `51525354555657585960616263646566676869707172` ```    }     private void insertOrder() throws SQLException {         PreparedStatement prep = conn.prepareStatement("insert into orders(customer_id , total) values(?, ?)");         prep.setInt(1, random.nextInt(getCustomerCount()));         BigDecimal total = new BigDecimal("0");         prep.setBigDecimal(2, total);         prep.executeUpdate();         ResultSet rs = prep.getGeneratedKeys();         rs.next();         int orderId = rs.getInt(1);         int lines = random.nextInt(20);         for (int i = 0; i < lines; i++) {             insertLine.setInt(1, orderId);             insertLine.setInt(2, i);             insertLine.setString(3, ITEMS[random.nextInt(ITEMS.length)]);             BigDecimal amount = new BigDecimal(random.nextInt(100) + "." + random.nextInt(10));             insertLine.setBigDecimal(4, amount);             total = total.add(amount);             insertLine.addBatch();         }         insertLine.executeBatch(); ```
View Full Code Here

 `341342343344345346347348349350351` ```    private void testPrecision() throws SQLException {         Connection conn = getConnection("functions");         Statement stat = conn.createStatement();         stat.execute("create alias no_op for \""+getClass().getName()+".noOp\"");         PreparedStatement prep = conn.prepareStatement("select * from dual where no_op(1.6)=?");         prep.setBigDecimal(1, new BigDecimal("1.6"));         ResultSet rs = prep.executeQuery();         assertTrue(rs.next());         stat.execute("create aggregate agg_sum for \""+getClass().getName()+"\"");         rs = stat.executeQuery("select agg_sum(1), sum(1.6) from dual"); ```
View Full Code Here

 `1057105810591060106110621063` ```    public void add(Object value) {         // ignore     }     public Object getResult() {         return new BigDecimal("1.6");     } ```
View Full Code Here

 `435436437438439440441` ```    }     // Source = BIGDECIMAL         public void testBigDecimalToString() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "string", "char(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `439440441442443444445` ```    public void testBigDecimalToString() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "string", "char(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     public void testBigDecimalToBoolean() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "boolean", "CASE WHEN 1.0 = 0 THEN 0 WHEN 1.0 IS NOT NULL THEN 1 END"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here