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

 `467468469470471472473` ```    @Test public void testBigDecimalToBigInteger() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "biginteger", "trunc(1.0)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     @Test public void testBigDecimalToFloat() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "float", "1.0"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

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

 `949950951952953954955` ```    @Test public void testDoubleToBigDecimal() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new Double(1.2), Double.class), "bigdecimal", "cast(1.2 AS numeric(38, 19))"); //\$NON-NLS-1\$ //\$NON-NLS-2\$     }     @Test public void testBigDecimalToLong() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "long", "cast(1.0 AS numeric(19,0))"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

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

 `957958959960961962963` ```    @Test public void testBigDecimalToBigInteger() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "biginteger", "cast(1.0 AS numeric(38, 0))"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     @Test 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

 `961962963964965966967` ```    @Test 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\$     }     @Test public void testBigDecimalToDouble() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "double", "cast(1.0 AS double precision)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `241242243244245246247248249250` ```     *      * @throws Exception      */     public void testTwoBigDecConst() throws Exception {         Expression[] args = new Expression[] {                 LANG_FACTORY.createLiteral(new BigDecimal("10"), BigDecimal.class), //\$NON-NLS-1\$                 LANG_FACTORY.createLiteral(new BigDecimal("6"), BigDecimal.class) //\$NON-NLS-1\$         };         helpTestMod(args, "(10 - (sign(10) * floor(abs((10 / 6))) * abs(6)))"); //\$NON-NLS-1\$     } ```
View Full Code Here

 `310311312313314315316317318319` ```     * @throws Exception      */     public void testOneBigDecElemOneBigDecConst() throws Exception {         Expression[] args = new Expression[] {                 LANG_FACTORY.createColumnReference("e1", null, null, BigDecimal.class), //\$NON-NLS-1\$                 LANG_FACTORY.createLiteral(new BigDecimal(6), BigDecimal.class)         };         // default / default         helpTestMod(args, "(e1 - (sign(e1) * floor(abs((e1 / 6))) * abs(6)))"); //\$NON-NLS-1\$     } ```
View Full Code Here

 `115116117118119120121` ```    }     // Source = BIGDECIMAL         @Test public void testBigDecimalToByte() throws Exception {         helpTest(LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "byte", "cast(1.0 AS tinyint)"); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `81828384858687` ```          }         }     }            @Test public void testBigDecimalToBigInteger_Defect16875() throws TransformationException {         helpTestTransform(new BigDecimal("0.5867"), new BigInteger("0")); //\$NON-NLS-1\$ //\$NON-NLS-2\$     } ```
View Full Code Here