BigDecimal相关总结
package bigInteger;
import java.math.BigInteger;
public class Test {
public static void main(String[] args) {
// 构造方法
System.out.println("=========构造方法=========");
System.out.println(new BigInteger("12361265130"));
System.out.println(BigInteger.valueOf(65413));
// 16进制的32 = 50
System.out.println(new BigInteger("32", 16));
// 运算
System.out.println("=========运算=========");
BigInteger a = new BigInteger("35418415365613");
BigInteger b = new BigInteger("35454865103546");
System.out.println(a + " + " + b + " = " + a.add(b));
System.out.println(a + " - " + b + " = " + a.subtract(b));
System.out.println(a + " × " + b + " = " + a.multiply(b));
System.out.println(b + " ÷ " + a + " = " + b.divide(a));
System.out.println(b + " ÷ " + a + " = " + b.divideAndRemainder(a)[0] + " .... " + b.divideAndRemainder(a)[1]);
// 取余
System.out.println(b + "÷" + a + " 余 " + b.remainder(a));
System.out.println(b + "÷" + a + " 余 " + b.mod(a));
// 绝对值
System.out.println(new BigInteger("-561456").abs());
// 相反数
System.out.println(new BigInteger("165").negate());
// 最大公约数
System.out.println(new BigInteger("36").gcd(new BigInteger("48")));
// 幂运算
System.out.println(new BigInteger("2").pow(10));
// 最大值最小值
System.out.println(a.max(b));
System.out.println(a.min(b));
// 判断相等
System.out.println(a.equals(a));
System.out.println(a.equals(b));
System.out.println(new BigInteger("1").equals(new BigInteger("1")));
System.out.println(new BigInteger("18963").equals(new BigInteger("18963")));
// 比较大小
System.out.println(a.compareTo(b));
// hash值
System.out.println("73246598的hash值" + new BigInteger("73246598").hashCode());
// 基本常量
System.out.println("=========基本常量=========");
System.out.println("BigInteger.ZERO = " + BigInteger.ZERO);
System.out.println("BigInteger.ONE = " + BigInteger.ONE);
System.out.println("BigInteger.TEN = " + BigInteger.TEN);
}
}
控制台输出
=========构造方法=========
12361265130
65413
50
=========运算=========
35418415365613 + 35454865103546 = 70873280469159
35418415365613 - 35454865103546 = -36449737933
35418415365613 × 35454865103546 = 1255755138969169794692763698
35454865103546 ÷ 35418415365613 = 1
35454865103546 ÷ 35418415365613 = 1 .... 36449737933
35454865103546÷35418415365613 余 36449737933
35454865103546÷35418415365613 余 36449737933
561456
-165
12
1024
35454865103546
35418415365613
true
false
true
true
-1
73246598的hash值73246598
=========基本常量=========
BigInteger.ZERO = 0
BigInteger.ONE = 1
BigInteger.TEN = 10
Process finished with exit code 0