The example in this article describes the simple usage of the BigDecimal class in Java. It is a very practical skill in Java programming. It is shared with everyone for your reference. The specific usage analysis is as follows:
Generally speaking, when it comes to business calculations in Java, we all know that float and double cannot be used because they cannot perform precise calculations. However, the designers of Java provide programmers with a very useful class BigDecimal, which can improve the shortcomings of float and double classes that cannot perform precise calculations. The BigDecimal class is located under the java.maths class package. First let's look at how to construct a BigDecimal object. It has many constructors. Here are the two most commonly used ones to demonstrate: one is BigDecimal(double val) and the other is BigDecimal(String str). There doesn't seem to be much difference between the two, but as the API description says:
/*The results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances nonwithstanding.The (String) constructor, on the other hand, is perfectly predictable : new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.*/That is to say, a constructor using double as a parameter cannot accurately construct a BigDecimal object. You need to specify a context environment, that is, specify the precise bit. The constructor that uses a String object as a parameter can accurately construct a BigDecimal object. Please look at the code below:
import java.math.*;public class TestBigDecimal { public static void main(String args[]){ BigDecimal bd = new BigDecimal("10.123"); BigDecimal bd1 = new BigDecimal(10.123); System.out.println(bd + "/n"+ bd1); }}Output after running:
10.123
10.1229999999999993320898283855058252811431884765625
Therefore, when we choose a constructor, it depends on the specific needs.
In addition, many people will ask how to convert basic types, such as int, float, double, long, and BigDecimal objects to and from each other. Very simple:
Basic types are converted into corresponding BigDecimal objects through constructors, and the BigDecimal class provides methods such as intValue(), floatValue(), doubleValue(), and longValue() to convert BigDecimal objects into corresponding values.
Regarding how BigDecimal is calculated, I took a person's question post in the forum as an example to briefly write down the calculation method of BigDecimal. The topic is: Li Bai was walking on the street without incident, carrying a pot to buy wine. When you encounter a shop, you double the amount, when you see flowers, you drink a dou. When you meet flowers and a shop, you drink up all the wine in the pot. How many dou of wine are there in Li Bai's pot?
This question should be pushed forward from back to front, and the calculation should be reversed, and finally the volume of the original wine can be obtained.
import java.math.*;public class Libai { public static void main(String args[]){ BigDecimal volumn = new BigDecimal("0"); for (int i=0; i<5; i++){ volumn = volumn .add(new BigDecimal("1")); volumn = volumn.divide(new BigDecimal("2")); } System.out.print(volumn); }}Running results:
0.96875
I hope that what this article describes will be helpful to everyone's learning of Java programming.