Common Java APIs random number BigInteger & BigDecimal math

Common Java APIs random number BigInteger & BigDecimal math

Random

Random number, starting from 0

import java.util.Random;
public class Random_01 {
public static void main(String[]args){
	//Create random number generator
	Random random = new Random();
	//Randomly generate once from 0 ~ 4
	int i = random.nextInt(5);
	System.out.println(i);
	//Generate 10 ~ 20
	//Nextint (max min + 1) + min
	int x = random.nextInt(11)+10;
	System.out.println(x);
	//Generate A~Z
	int b = random.nextInt(25)+65;
	char c = (char)b;//Type conversion
	System.out.println(c);		
}
}

Number

java.text.DecimalFormat: number formatting
#Any number 0-9 single number
, thousandth
. decimal point
0 complement

import java.text.DecimalFormat;
public class Number_01 {
public static void main (String[]args){
	//Create a number formatting object
	//Join the thousandth percentile
	DecimalFormat df = new DecimalFormat("###,###");
	System.out.println(df.format(234567890));
	//Add the thousandth place and keep two decimal places
	df = new DecimalFormat("###,###.##");
	//Support rounding
	System.out.println(df.format(22222.65678));
	//Add the thousandth fraction as, keep 4 decimal places, and make up 0 if it is not enough
	df = new DecimalFormat(",###.0000");
	System.out.println(df.format(234567654.987));
}
}

java.lang.Math provides a series of static methods for scientific calculation. The parameters and returns of its methods
The value type is usually double.
abs absolute value acos,asin,atan,cos,sin,tan square root of trigonometric function sqrt
log natural logarithm of power b of pow (double a, double b) a
exp e is the bottom index
max(double a,double b)
min(double a,double b)
random() returns a random number from 0.0 to 1.0
Long round (double a) data a of double type is converted to long type (rounded)
Todegrees (double angle) radians - > angle
toRadians(double angdeg) angle - > radians

BigInteger and BigDecimal classes

BigInteger
1. As the wrapper class of int, the Integer class can store a maximum Integer value of 231-1, and the Long class is also limited, with a maximum of 263-1. If you want to represent a large Integer, neither the basic data type nor their wrapper class can do anything, let alone operate.

2,java. The BigInteger of the math package can represent an immutable integer of arbitrary precision. BigInteger provides
The equivalent of all Java's basic integer operators, and provides Java All relevant methods of lang. math. In addition, BigInteger also provides the following operations: modular arithmetic, GCD calculation, prime test, prime generation, bit operation, and some other operations.

3. Constructor
BigInteger(String val): build BigInteger objects based on strings

4. Common methods
public BigInteger abs(): BigInteger that returns the absolute value of this BigInteger.
BigInteger add(BigInteger val): returns the BigInteger whose value is (this + val)
BigInteger subtract(BigInteger val): returns the BigInteger whose value is (this - val)
BigInteger multiply(BigInteger val): returns a BigInteger with a value of (this * val)
BigInteger divide(BigInteger val): returns a BigInteger with a value of (this / val). Integer division preserves only the integer part.
BigInteger remaining (BigInteger VAL): returns a BigInteger with a value of (this% VAL).
BigInteger [] divideandremander (BigInteger VAL): returns an array containing two BigIntegers (this / val) followed by (this% VAL).
BigInteger pow(int exponent): returns a BigInteger whose value is (thisexponent).

BigDecimal
The general Float class and Double class can be used for scientific calculation or engineering calculation, but in commercial calculation, it requires high digital accuracy, so Java math. BigDecimal class.
The BigDecimal class supports immutable signed decimal points of arbitrary precision.
constructor
public BigDecimal(double val)
public BigDecimal(String val)
common method
public BigDecimal add(BigDecimal augend)
public BigDecimal subtract(BigDecimal subtrahend)
public BigDecimal multiply(BigDecimal multiplicand)
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)

java.math.BigInteger and bigDecimal
Very high accuracy
example

import java.math.BigDecimal;
import java.math.BigInteger;
public class BigDecimal_01 {
public static void main(String[]args){
	//When creating this object, you need to pass the number of string
	BigInteger bi = new BigInteger("22");
	BigDecimal v1 = new BigDecimal(20);
	BigDecimal v2 = new BigDecimal(20);
	//+Not v1+v2, not because it is a reference type
	BigDecimal v3 = v1.add(v2);
	//-
	v3 = v1.subtract(v2);
	//*
	v3 = v1.multiply(v2);
	// /
	v3 = v1.divide(v2);
	//%
	v3 = v1.remainder(v2);
	System.out.println(v3);
}
}

Factorial

import java.math.BigDecimal;
public class BigDecimal_02 {
public static void main(String[]args){
	long a = m1(20);
	System.out.println(a);
	//BigDecimal b = new BigDecimal("");  error
	BigDecimal b = m2(211);
	System.out.println(b);
}
//The value of traditional recursive long type is limited
public static long m1(long n){
	if(n<=1){
		return 1;
	}
	return n*m1(n-1);	
}
public static BigDecimal m2(int n){
	if(n<=1){
		return new BigDecimal(1);
	}
	return new BigDecimal(n).multiply(m2(n-1));	
}	
}

Math

Matht provides scientific calculation and basic digital operation methods
All of them are static methods, which can be called by the class name, and in Math and Java No need to guide package under Lang
common method

public class Math_01 {
public static void main (String[]args){
	//abs absolute value
	System.out.println(Math.abs(-23));
	//ceil rounded up, decimal greater than 0 + carry
	System.out.println(Math.ceil(1.00000001));
	//floor rounding down and discarding decimals
	System.out.println(Math.floor(1.90239));
	//Square root sqrt
	System.out.println(Math.sqrt(4));
	//Cube root cbrt
	System.out.println(Math.cbrt(8));
	//Random random number generation is greater than or equal to 0.0 and less than 1.0
	System.out.println(Math.random());
	//rint rounding
	//Banking algorithm, rounding, double or even
	System.out.println(Math.rint(2.5));
	System.out.println(Math.rint(3.5));
	//pow N power
	System.out.println(Math.pow(2, 3));
}
}

Added by tonypr100 on Mon, 17 Jan 2022 00:58:35 +0200