Java Bigdecimal type operation, javabigdecimal

Source: Internet
Author: User

Java Bigdecimal type operation, javabigdecimal

Bigdecimal type operation in Java

 

Double-precision floating-point variable can process 16-bit valid numbers. In practical applications, You need to calculate and process larger or smaller numbers. The API class BigDecimal provided by Java in the java. math package is used to accurately calculate the number of valid 16 bits. Table 5.7 lists the main constructors and methods of the BigDecimal class.

 

Constructor description

BigDecimal (int) creates an object with the integer specified by the parameter.

BigDecimal (double) creates an object with the double precision value specified by the parameter.

BigDecimal (long) creates an object with the Length Integer specified by the parameter.

BigDecimal (String) creates an object with the value specified by the parameter in the String.

 

 

Method description

Add (BigDecimal) values in the BigDecimal object are added, and then this object is returned.

Subtract (BigDecimal) values in the BigDecimal object subtract, and then return this object.

Multiply the values in the multiply (BigDecimal) BigDecimal object and return this object.

Divide (BigDecimal) values in the BigDecimal object are divided, and then this object is returned.

ToString () converts the value of the BigDecimal object to a string.

DoubleValue () returns the values in the BigDecimal object with double precision.

FloatValue () returns the values in the BigDecimal object with a single precision.

LongValue () returns the value in the BigDecimal object as a long integer.

IntValue () returns the value in the BigDecimal object as an integer.

 

Note: Generally, because the numeric type, such as double, cannot accurately represent a number greater than the 16-bit valid number, it makes sense to use the BigDecimal (String) constructor to create an object. In addition, BigDecimal creates an object. We cannot directly perform mathematical operations on the object using traditional arithmetic operators such as +,-, *, And, instead, the corresponding method must be called. The parameters in the method must also be BigDecimal objects.

 

Eg:

Multiply two BigDecimal data types:

BigDecimal a = newbigdecimal (15124 );
BigDecimal B = new BigDecimal (15124 );
BigDecimal c = a. multiply (B );

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.