Objective
It is well known that Java provides API class BigDecimal in the Java.math package, which is used to accurately compute the number of more than 16-bit significant digits. Double floating-point variable double can handle 16-bit valid numbers. In practical application, it is necessary to compute and process the larger or smaller number. float and double can only be used for scientific calculations or engineering calculations, which are used in commercial calculations java.math.BigDecimal .
BigDecimal created objects, we cannot use the traditional + 、-、 *,/and other arithmetic operators directly to the mathematical operations of its objects, but must call its corresponding method.
The parameter in the method must also be an BigDecimal object. Constructors are special methods of classes that are designed to create objects, especially objects with parameters.
The sample code is as follows
Import Java.math.BigDecimal;
public class T {public static void main (string[] args) {String a = "9999.9999";
int b = 9999;
Double C = 9999.9999;
Char d = 99;
System.out.println ("===================");
Different types convert to BigDecimal BigDecimal ma = new BigDecimal (a);
BigDecimal MB = new BigDecimal (b);
BigDecimal mc = new BigDecimal (c);
BigDecimal MD = new BigDecimal (d);
System.out.println ("Ma:" +ma.tostring ());
System.out.println ("MB:" +mb.tostring ());
System.out.println ("MC:" +mc.tostring ());
SYSTEM.OUT.PRINTLN ("MD:" +md.tostring ());
System.out.println ("===================");
Add BigDecimal add = ma.add (MB);
System.out.println ("addition:" +add);
Subtract BigDecimal sub = ma.subtract (MB);
System.out.println ("Subtraction:" +sub);
Multiply BigDecimal Mul = mb.multiply (MD);
System.out.println ("multiplication:" +mul);
except BigDecimal div = mb.divide (MD);
SYSTEM.OUT.PRINTLN ("Division:" +div);
System.out.println ("===================");
MC = Mc.setscale (2, bigdecimal.round_half_up);
System.out.println ("rounded:" +MC); SyStem.out.println ("===================");
MC = Mc.negate ();
SYSTEM.OUT.PRINTLN ("Negative number:" +MC);
System.out.println ("==================="); }
}
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.