#### Lead

The use of mathematical operations, in which BigInteger and BigDecimal can operate large numbers. The bottom is the focus, to achieve accurate four, five into operation.

# primary coverage

- Math class
- Random class
- Big numbers operation class

# concrete content

### Math class

Math is a special operation class for mathematical calculation, which provides a series of mathematical calculation methods. All methods provided by

within the Math class are static – type, because there is no general attribute in the Math class.

Math has one way to draw attention:

, four, five: public, static, long, round (double, a).

example: observe four homes five

Public class TestDemo static void main {public (String args[]) {System.out.println (Math.round (15.5)); System.out.println (Math.round (-15.5)); System.out.println (Math.round (-15.51));}}

Output result

16-15 -16

If the negative number four, five into the time, the operation of data small digits greater than 0.5 to carry, less than or equal to 0.5 non carry.

### Random class

The main function of this class is to obtain the operation class of random numbers.

Example: produces 10 positive integers less than 100 (0~99)

Public class TestDemo static void main {public (String args[]) {Random Rand = new (Random); for (int i = 0; I 10; < i++) {System.out.print (rand.nextInt (100) + "");}}}

The output may be

53, 31, 77, 51, 68, 23, 59,, 63, 27, and 93,

### Large integer operation class: BigInteger

If the value of the data to operate now is great, then the first thought should be double, so what if the result of the calculation is now more than double?.

Public, class, TestDemo, {public, static, void, main (String, args[]) {System.out.println (Doublic.MAX_VALUE * Doublic.MAX_VALUE)}}

Output result

Infinity

It is now found that the results of this calculation do not exist because they have exceeded the scope of double.

interview question: can you tell me the current hypothesis that there are two very large numbers to be mathematically calculated (over the doulble range)? What should you do?

if you really exceed the double range, then you certainly can’t use double to save, only String type to save. If the really large numbers of data are to be mathematically computed, they can only be changed to String, and then the data saved by each character is computed manually.

so, in the Java which takes into account such cases, specifically to provide a large number of operations classes, including BigInteger, BigDecimal two.

BigInteger class construction method: public BigInteger (String VAL), it receives the String type.

Public class TestDemo static void main {public (String args[]) {BigInteger bigA = new BigInteger ("124123412141234"); BigInteger bigB = new BigInteger ("987654345"); System.out.println ("add operation:" + (bigA.add (bigB))); System.out.println ("subtraction operation:" + (bigA.subtract (bigB))); System.out.println ("multiplication" + (bigA.multiply (bigB))); System.out.println ("division" + (bigA.divide (bigB))); / / array has only two elements, the first element that is taking, the second element represents the remainder BigInteger result[] = bigA.divideAndRemainder (bigB); System.out.println (":" + result[0] + "remainder" + result[1]);}}

Output result

Addition operation: 124124399795579 subtraction operation: 124122424486889 multiplication operation: 122591027317515513761730 division operation: 125674 quotient: 125674 remainder: 939987704

In the Java while providing a large number of operations, but most of the time, we may use the project development for digital requirements will be more sensitive to numbers, this time Java itself provides is of little help.

should look for third parties at this time.

### Large floating point operations class: BigDecimal

BigInteger cannot save decimal numbers, and BigDecimal saves decimal data. There are several constructs available in BigDecimal:

- Structure 1: public BigDecimal (String val).
- Construct two: public BigDecimal (double val).

Like BigInteger, BigDecimal itself supports basic mathematical calculations, but there is a very important purpose of using BigDecimal, which is to use it for accurate four to five operations.

Previously used Math.round () to achieve four, five into the operation, but there is a problem with this operation, all the small numbers are four to five.

unfortunately, there is no direct support for the BigDecimal class with four in five, but you can use division implementation:

- Divide operations: public, BigDecimal, divide (BigDecimal, divisor, int, scale, int, roundingMode). BigDecimal divisor: dividend. Int scale: reserved decimal places. Int roundingMode: carry mode (public static final int ROUND_HALF_UP).

Example: accurate four to five

Public class MyMath {/ * * * to achieve accurate number four to five homes in the operation of @param num to four * five homes in the operation of digital scale * @param to retain the decimal * after @return treatment, four to five homes in double round / public static data (double num, int scale) {BigDecimal bigA = new (BigDecimal num; bigB = new) BigDecimal BigDecimal (1); return bigA.divide (bigB, scale, BigDecimal.ROUND_HALF_UP) (.DoubleValue);}} {public class TestDemo public static void main (String args[]) {System.out.println (MyMath.round (19.783231231, 2); (MyMath.round) System.out.println (-15.5, 0) (MyMath.round (System.out.println); 15.5, 0));}}

Output result

19.78 -16 16

The operation of the function in the development of the day must be used to support the category which belongs to the class of tools.

# summary

- The Math class is important to clear the pits of the round () method.
- The Random class generates random numbers.
- If the amount of data is large, use BigInteger or BigDecimal, and these two classes are subclasses of Number.

More content below:

- My story
- Java notes
- Android notes
- Design pattern notes
- Android difficult analysis
- Android essence tutorial