Java

# Java BigDecimal

The use of floating-point data formats in financial calculations is restricted due to their inaccuracy. That is why Java has a special class called BigDecimal to reduce these problems. The BigDecimal is of 32-bit integers to handle floating point numbers that contain large double values and extremely small with remarkable precision. The BigDecimal class offers methods for manipulation, format conversion, hashing, rounding, comparison, and arithmetic. Some class methods of BigDecimal are implemented below.

## Example 1:

BigDecimal is effective in representing numbers of almost infinite size. The BigDecimal holds the larger floating values whereas the BigInteger takes the large number values. The program is implemented below to show the functionality of the BigDecimal in Java. We have first included the “BigDecimal” and “BigInteger” packages inside the Java program. Then, we declared the object of BigInteger as “val1” and the BigDecimal object as “val2”. We have assigned the long integer value to the object “val1” of the BigInteger() class and the floating point value to the “val2” object of the BigDecimal() class. After that, we printed both values to accomplish the results.

The results of values BigInteger and BigDecimal are executed as follows: ## Example 2:

BigDecimal only allows arbitrary-precision integers of fixed-point numbers. Let’s have a java program where the arithmetic operations are performed on two significant large decimal integers. We have defined two BigDecimal objects which are labeled “num1” and “num2”. These BigDecimal objects are initialized with large decimal values. Then, we applied the addition, subtraction, and multiplication operation on these objects by using the add(), subtract() and multiply() methods, respectively. Note that the first object “num1” of BigDecimal called the method and the second object “num2” passed as an argument of that method.

The following results are accomplished by applying the above methods to the BigDecimal values. ## Example 3:

The comparison operation cannot be performed on BigDecimal values using the comparison operators. As, BigDecimal is restricted to int, long, and double data types. Java has a built-in compareTo() method for comparing BigDecimal values. We have a BigDecimal object “Big1” and “Big2” declaration inside the main() method of the specified Java class. After declaring the objects, we set the decimal values in them. Then, we used the nested “if-else” block for comparing the BigDecimal object using the compareTo() method. If both the BigDecimal object are equal, then the zero is returned and the print statement of the “if” section will be executed. Otherwise, the compareTo() method returns the value “1” when the BigDecimal contains different values. We checked the return results from the compareTo() method for the decimal values provided in the BigDecimal() class via nested if-else statement.

The compareTo() method value is equal to the value “1” which shows that the float numbers passed in the BigDecimal() class are not equal. The “else-if” print message is displayed below. ## Example 4:

Java has another built-in byteValueExact BigDecimal for the conversion of the BigDecimal to a byte while searching for any lost data. This method throws an ArithmeticException when the BigDecimal has a fractional part that is not zero, or if the result is beyond the range that can be stored by a byte. We have utilized the byteValueExact() method in the above program. For this, we have declared the variable “BigDec” of BigDecimal. Then, we defined a variable “b” of type byte. We have set the value within the BigDecimal() class which is initialized inside the “BigDec” variable. Next, we invoked the byteValueExact() in the variable “b” to get the byte value of the BigDecimal value. The byte value returned from this method will be printed with the aid of the Java print method.

The byte value from the byteValueExact() method is the same as the input BigDecimal value because the number contains no decimal point. ## Example 5:

The maximum and minimum values from the specified BigDecimal values can be accomplished using the max() and min() methods as these methods are compatible with the BigDecimal class. We have generated three BigDecimal objects as “b1”, “b2” and “b3” to get the minimum value and maximum values from these. Along with these objects’ declarations, we have also created two more objects “MaxVal1” and “Maxval2”. First, we passed the BigDecimal numbers to each of the objects. Within the “MaxVal1”, the object “b1” is called the max() method which takes the “b2” object as a parameter. In the same way, we have set the “MaxValue2” but deployed the “min()” method to find the minimum values among “b1” and “b3”. The max() method displayed the largest BigDecimal value and the min() method displayed the smallest BigDecimal value.

The BigDecimal value of “b2” contains the maximum value as compared to the “b1” which the max() method outputs below. The same is the case for the min() method which displays the BigDecimal value “b3” because it has a minimum value of “b1”. ## Example 6:

The pow() method of BigDecimal is used to get the power value of the provided BigDecimal value. A BigDecimal with the value (this n) is returned from the pow() method. We have specified the BigDecimal objects “bd1” and “bd2”. In the object “bd1”, we have assigned the value of the BigDecimal which contains the decimal point. Then, we applied the pow() method on the object “bd1” within the object “bd2”. The pow() method is set with the value “3” which returns the power of “3” for the BigDecimal value.

The BigDecimal value raised to the power “3” has the following value: ## Example 7:

The last BigDecimal method implemented below is called scale(). It is used to get the scale value of the provided BigDecimal. The scale of a zero or positive value is determined by the number of digits that are placed at the right of the decimal point. When a value is negative, the unscaled number is multiplied by the power of value “10” negation of its scale’s negation. We have given a positive float value to the BigDecimal which is initialized in the variable “deci1”. On the other hand, we have set the negative float value of the BigDecimal in the “deci2” object. Then, we have called the “scale()” method for the deci1 and the deci2 object within the newly declared variables “s1” and “s2” to get the scale of these BigDecimals.

The decimal value at the right side of positive BigDecimal is “1” so the scale is also “1”. The scale of the negative BigDecimal is “3”. ## Conclusion

Java BigDecimal is a method for precisely representing the number. The few methods associated with the BigDecimal are explored in this document. BigDecimal would prevent the different scale of the double value when dealing with the smaller double may be removed from the sum due to variation in scale. This is an advantage of the BigDecimal class but the drawback of BigDecimal is that it makes it more complicated to create algorithms. 