Java Basic Series 9:biginteger class and BigDecimal class

Source: Internet
Author: User

a BigInteger class

When we have to deal with a very large number, we must not be able to use int and long. Of course, we can use string to receive large numbers, and then split the way to calculate, but this way is cumbersome. Therefore, in order to solve this problem in Java, the BigInteger class is provided. The BigInteger class represents a large integer class, defined in the java.math.* package, if the operation of an integer already exceeds the maximum type length of the integer long, then consider using the BigInteger class to operate

examples of commonly used methods:

package javase.base;import java.math.biginteger;public  Class bigintegerdemo {public static void main (String[] args)  {BigInteger  binteger1 = new biginteger ("1234567890"); Biginteger binteger2 = new biginteger ("222222222"); System.out.println ("Add:"  + binteger1.add (BInteger2)); System.out.println ("Minus: "  + binteger1.subtract (BInteger2)); System.out.println ("Multiply: "  + binteger1.multiply (BInteger2)); System.out.println ("except: "  + binteger1.divide (BInteger2)); System.out.println ("Larger value: "  + binteger1.max (BInteger2)); System.out.println ("Small: "  + binteger1.min (BInteger2)); Biginteger result[] = binteger1.divideandremainder (BINTEGER2); //  division operation, The result arrays are quotient and remainder System.out.println ("quotient: "  + result[0] +  ", remainder: "  + result[1]) ;}}

Output:

Add: 1456790112 minus: 1012345668 times: 274348419725651580 except: 5 larger: 1234567890 Lower: 222222222 quotient: 5, remainder: 123456780

two BigDecimal class

For calculations that do not require precise results, you can use float and double, but to get a very precise result, you need to use the BigDecimal class, while the BigDecimal class can do a large number of operations.

Examples of common methods:

package javase.base;import java.math.bigdecimal;public  Class bigdecimaldemo {public static void main (String[] args)  {double  d1 = 233.333;double d2 = 10.24; Bigdecimal bigdecimal1 = new bigdecimal (D1); Bigdecimal bigdecimal2 = new bigdecimal (D2); System.out.println ("Plus: "  + bigdecimal1.add (BigDecimal2). Doublevalue ()); System.out.println ("Minus: "  + bigdecimal1.subtract (BigDecimal2). Doublevalue ()); System.out.println ("Multiply: "  + bigdecimal1.multiply (BigDecimal2). Doublevalue ());//reserved 8 decimal places, Rounding mode System.out.println ("except: "  + bigdecimal1.divide (bigdecimal2, 8, ) rounded to the nearest number direction BIGDECIMAL.ROUND_HALF_UP)); System.out.println ("No Rounding results: "  + bigdecimal1.add (BigDecimal2));}} 

Output:

Add: 243.573 minus: 223.093 times: 2389.32992 except for: 22.78642578 results without rounding: 243.5729999999999986215470926254056394100189208984375

Note: If you want to compare twoBigInteger Type orvariables of type BigDecimal are equal and need to be used as a string type. Equals () method to compare, rather than simply use = = to compare, such as:

public static void Main (string[] args) {double d2 = 10.24; BigDecimal bigDecimal2 = new BigDecimal (D2); BigDecimal bigDecimal3 = new BigDecimal (D2), if (BigDecimal3 = = BigDecimal2) System.out.println ("= = to compare"); if ( Bigdecimal3.equals (BigDecimal2)) System.out.println (". Equals () to compare");}

Output:

. Equals () to compare


This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1758640

Java Basic Series 9:biginteger class and BigDecimal class

Related Article

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.