目录
1、Math类位于java.lang包中,是一个final类,不能被继承
2、Math类中所有方法都是static方法,直接使用类名.Math调用
Math
double d=-9.8;
System.out.println(Math.ceil(d));/*向上取整 -9.0*/
System.out.println(Math.floor(d));/*向下取整 -10.0*/
System.out.println(Math.round(d));/*四舍五入返回最近参数(保留整数部分) 0.5舍去 -10*/
System.out.println(Math.round(-9.5));/*-9*/
System.out.println(Math.abs(d));/*绝对值 9.8*/
System.out.println(Math.max(2,5));/*最大值 5*/
System.out.println(Math.min(4.9,1));/*最小值 1.0*/
System.out.println(Math.pow(2,3));/*2^3=2*2*2=8.0*/
System.out.println(Math.sqrt(16));/*4.0*/
System.out.println(Math.random());/*随机值0.0~1.0*/
BigInteger
Java中整数最大范围是long型,64位,如果需要使用超过long范围的大整数,可以使用BigInteger类;
BigInteger位于java.math包中,定义了一系列的数学运算方法,调用这些方法可以进行计算,不能使用运算符计算;
import java.math.BigInteger;
public class BigIntegerTest {
public static void main(String[] args) {
String s1 = "3213213123322342342380808999798";
String s2 = "5213213123325342342380808999799";
//将字符串作为参数创建BigInteger对象
BigInteger bt1 = new BigInteger(s1);
BigInteger bt2 = new BigInteger(s2);
System.out.println("大数相加:" + bt1.add(bt2));
System.out.println("大数相减:" + bt1.s