BigDecimal is used in Java for floating point arithmetic

  • 2020-04-01 03:26:17
  • OfStack

Recently studied the Java floating point number calculation problem, from the Internet to query the relevant information, summarized and after some sorting and debugging, finally complete this article, welcome to point out the errors and problems.
In Java, the variables declared by float are single-precision floating-point Numbers, and the variables declared by double are double-precision floating-point Numbers. As the name implies, a double entity occupies twice as much memory space as a float. Float is 4 bytes and double is 8 bytes. Data of float and double types cannot accurately represent the result of the calculation, because float and double are inaccurate calculations. You can see this in the following code:


public class Test
{
public static void main(String[] args)
{
System.out.println(0.05 + 0.01);
System.out.println(1.0 - 0.42);
System.out.println(4.015 * 100);
System.out.println(123.3 / 100);
}
}


The result of operation is:
0.060000000000000005
0.5800000000000001
401.49999999999994
1.2329999999999999

To get the desired effect, try using java.text.decimalformat to format floating point Numbers:
DecimalFormat can format Numbers in a certain format. The commonly used format characters are #, 0, etc. Ex. :


System.out.println(new java.text.DecimalFormat("0.00").format(3.125));
System.out.println(new java.text.DecimalFormat("0.00").format(3.135));


But the result is:
3.12
3.14

This is because DecimalFormat rounds half-even (ROUND_HALF_EVEN) to the nearest even number when rounding 5. So even using DecimalForamt does not yield reliable floating point Numbers. Finally, we can consider using BigDecimal for more accurate calculations:

BigDecimal provides multiple constructors related to floating point Numbers:


BigDecimal(double val)  Translates a double into a BigDecimal.
BigDecimal(String val)  Translates the String repre sentation of a BigDecimal into a BigDecimal.

However, if you create an object with a double parameter to get an inaccurate value, it is only accurate to create an object with a String.

Such as:


BigDecimal bd1=new BigDecimal(0.05);
System.out.println(bd1.toString());
BigDecimal bd2=new BigDecimal("0.05");
System.out.println(bd2.toString());

Results:

0.05000000000000000277555756156289135105907917022705078125
0.05

So we end up using String to create the object so that the result is the most accurate. Also, if it's a double, we can use: BigDecimal. ValueOf (double val) for the simple reason that the JDK source code looks like this:


public static BigDecimal valueOf(double val)
{
return new BigDecimal(Double.toString(val));
}

Finally, it should be noted that the addition, subtraction, multiplication and division of BigDecimal will eventually return a new BigDecimal object, because BigDecimal is immutable, and a new object will be generated at each operation step. Although the addition operation is done, a does not save the added value. The correct usage should be a= a.dd (b).


Related articles: