When we are in the process of development, double or float for the subtraction operation, there will be a case of loss of precision
In the following example, we can see the accuracy of the
|
Demo |
|
public class Bigdecimaldemo {
public static void Main (string[] args) {
System.out.println (0.09 + 0.01);
System.out.println (1.0-0.32);
SYSTEM.OUT.PRINTLN (1.015 * 100);
System.out.println (1.301/100);
System.out.println (1.0-0.12);
} |
This time, for the above situation, we introduce the object
BigDecimal class:
immutable, arbitrary-precision signed decimal numbers can be used to solve data loss problems.
Public BigInteger Add (BigInteger val): plus
Public BigInteger Subtract (BigInteger val): minus
Public BigInteger Multiply (BigInteger val): Multiply
Public BigInteger Divide (BigInteger val): except
Public biginteger[] Divideandremainder (BigInteger val): returns an array of quotient and remainder
|
Demo |
|
public class Bigintegerdemo {
public static void Main (string[] args) {
BigInteger bi1 = new BigInteger ("100");
BigInteger Bi2 = new BigInteger ("50");
Public BigInteger Add (BigInteger val): plus
System.out.println ("Add:" + bi1.add (BI2));
Public BigInteger Subtract (BigInteger val): plus
System.out.println ("Subtract:" + bi1.subtract (BI2));
Public BigInteger Multiply (BigInteger val): plus
System.out.println ("Multiply:" + bi1.multiply (BI2));
Public BigInteger Divide (BigInteger val): plus
System.out.println ("Divide:" + bi1.divide (BI2));
Public biginteger[] Divideandremainder (BigInteger val): returns an array of quotient and remainder
biginteger[] bis = bi1.divideandremainder (BI2);
System.out.println (" quotient:" + bis[0]);
System.out.println (" remainder:" + bis[1]);
}
}
|
perfectly solves the problem of float type and double loss accuracy.
Common objects in Java--bigdecimal