PackageCom.lei.duixiang;ImportJava.math.BigDecimal;ImportJava.math.BigInteger; Public classBignum {/*** Large number operation (BigInteger, BigDecimal) * difference: BigDecimal added the concept of decimal, it supports the fixed-point number of any precision *@paramargs*/ //large number operation (BigInteger) Public Static voidBigintegerdemo () {BigInteger BigInteger=NewBigInteger ("4");//instantiate a large number//take this large number plus 2 operationSYSTEM.OUT.PRINTLN ("addition Operation:" +biginteger.add (NewBigInteger ("2"))); //take this large number minus 2 operations.System.out.println ("Subtraction Operation:" +biginteger.subtract (NewBigInteger ("2"))); //take the large number by the 2 operationSystem.out.println ("Multiplication Operation:" +biginteger.multiply (NewBigInteger ("2"))); //take the large number except for the 2 operationSYSTEM.OUT.PRINTLN ("Division Operation:" +biginteger.divide (NewBigInteger ("2"))); //take the big number in addition to 3 of the quotientSystem.out.println ("Pick-up:" +biginteger.divideandremainder (NewBigInteger ("3")) [0]); //take the remainder of the large number except 3System.out.println ("Pick-up:" +biginteger.divideandremainder (NewBigInteger ("3")) [1]); //take the big number 2 timesSystem.out.println ("Do 2-square operation:" +biginteger.pow (2)); //take the opposite number of the large numberSystem.out.println ("Take the opposite number of actions:" +biginteger.negate ()); } //large number operation (BigDecimal) Static Final intLocation = 10; /*** Define addition method with parameters of Addend and Summand *@paramargs*/ PublicBigDecimal Add (DoubleD1,DoubleD2) {//addition//instantiating a Decimal objectBigDecimal B1 =NewBigDecimal (double.tostring (D1)); BigDecimal B2=NewBigDecimal (double.tostring (D2)); returnB1.add (B2);//Call the addition method } PublicBigDecimal Sub (DoubleD1,DoubleD2) {//Subtraction//instantiating a Decimal objectBigDecimal B1 =NewBigDecimal (double.tostring (D1)); BigDecimal B2=NewBigDecimal (double.tostring (D2)); returnB1.subtract (B2);//calling the subtraction method } PublicBigDecimal Mul (DoubleD1,DoubleD2) {//multiplication//instantiating a Decimal objectBigDecimal B1 =NewBigDecimal (double.tostring (D1)); BigDecimal B2=NewBigDecimal (double.tostring (D2)); returnB1.multiply (B2);//call the Multiplication method } PublicBigDecimal Div (DoubleD1,DoubleD2) {//Division returnDiv (d1,d2,location);//calling a custom division method } //define the starting method, the parameters are the divisor and the divisor, and the number of digits after the quotient decimal point PublicBigDecimal Div (DoubleD1,DoubleD2,intb) { if(b < 0) {System.out.println ("B value must be greater than or equal to 0"); } BigDecimal B1=NewBigDecimal (double.tostring (D1)); BigDecimal B2=NewBigDecimal (double.tostring (D2)); //Call the division method, keep the B-bit after the decimal point, and round the result to the Operation returnB1.divide (B2, B, bigdecimal.round_half_up); } //Division Public Static voidMain (string[] args) {//large number operation (BigInteger)Bigintegerdemo (); System.out.println ("--------------------"); Bignum b=NewBignum (); System.out.println ("Two numbers Add Result:" +b.add (-7.5, 8.9)); System.out.println ("Two digit subtraction Result:" +b.sub (-7.5, 8.9)); System.out.println ("Two numbers Multiply results:" +b.mul (-7.5, 8.9)); System.out.println ("Two numbers divide the result, and the result is 10 bits after the decimal:" +b.div (10, 2)); System.out.println ("Divide two numbers and leave 5 decimal places:" +b.div (-7.5, 8.9,5)); }}
16, large number operation (BigInteger, BigDecimal)