Basic usage of subtraction in Java BigDecimal _java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.