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

 `443444445446447448449` ```    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\$     }     public void testBigDecimalToByte() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "byte", "smallint(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `447448449450451452453` ```    public void testBigDecimalToByte() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "byte", "smallint(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     public void testBigDecimalToShort() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "short", "smallint(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `451452453454455456457` ```    public void testBigDecimalToShort() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "short", "smallint(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     public void testBigDecimalToInteger() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "integer", "integer(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `455456457458459460461` ```    public void testBigDecimalToInteger() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "integer", "integer(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     public void testBigDecimalToLong() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "long", "bigint(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `459460461462463464465` ```    public void testBigDecimalToLong() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "long", "bigint(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     public void testBigDecimalToBigInteger() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "biginteger", "cast(1.0 AS numeric(31,0))"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `463464465466467468469` ```    public void testBigDecimalToBigInteger() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "biginteger", "cast(1.0 AS numeric(31,0))"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     public void testBigDecimalToFloat() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "float", "cast(1.0 AS real)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `467468469470471472473` ```    public void testBigDecimalToFloat() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "float", "cast(1.0 AS real)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     public void testBigDecimalToDouble() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "double", "double(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `444546474849505152` ```   * @return Outgoing value of target type    * @throws TransformationException if value is an incorrect input type or    * the transformation fails    */   public Object transformDirect(Object value) throws TransformationException {     BigDecimal result = BigDecimal.valueOf(((Number)value).doubleValue());     result = result.setScale(Math.max(result.scale(), (value instanceof Double ? 16 : 8) - result.precision()));     return result;   } ```
View Full Code Here

 `83848586878889909192` ```            testType(i);         }     }     private void testCompare() {         ValueDecimal a = ValueDecimal.get(new BigDecimal("0.0"));         ValueDecimal b = ValueDecimal.get(new BigDecimal("-0.00"));         assertTrue(a.hashCode() != b.hashCode());         assertFalse(a.equals(b));     } ```
View Full Code Here

 `139140141142143144145146147148149` ```        case Value.INT:             return ValueInt.get(random.nextInt());         case Value.LONG:             return ValueLong.get(random.nextLong());         case Value.DECIMAL:             return ValueDecimal.get(new BigDecimal(random.nextInt()));             // + "12123344563456345634565234523451312312"         case Value.DOUBLE:             return ValueDouble.get(random.nextDouble());         case Value.FLOAT:             return ValueFloat.get(random.nextFloat()); ```
View Full Code Here

## Related Classes of java.math.BigDecimal

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.