java Math类的常用方法介绍
代码块

public class MainTest {
public static void main(String[] args) {
//求sin值
double sin = Math.sin(3.14);
System.out.println("sin3.14=" + sin);
//求cos值
double cos = Math.cos(0);
System.out.println("cos0=" + cos);
//求tan值
double tan = Math.tan(0.785);
System.out.println("tan0.785=" + tan);
//求arcsin
double arcsin = Math.asin(1);
System.out.println("arcsin1 = " + arcsin);
//求arccos
double arccos = Math.acos(1);
System.out.println("arccos = " + arccos);
//求arctan
double arctan = Math.atan(30);
System.out.println("arctan30 = " + arctan);
//求弧度
double radians = Math.toRadians(180);
System.out.println("180度角的弧度为" + radians);
//求角度
double angle = Math.toDegrees(3.141592653589793);
System.out.println("π的角度数为" + angle);
//求以e为底的指数
double exp = Math.exp(1);
System.out.println("以e为底指数为1的数为" + exp);
//求以e为底e的平方的对数
double log = Math.log(Math.E * Math.E);
System.out.println("以e为底e的平方的对数" + log);
//求以10为底100的对数
double log10 = Math.log10(100);
System.out.println("以10为底100的对数" + log10);
//求100的平方根
double sqrt = Math.sqrt(100);
System.out.println("100的平方根是" + sqrt);
//求27的立方根
double cbrt = Math.cbrt(27);
System.out.println("27的立方根是" + cbrt);
//求10除以3的余数
double rest = Math.IEEEremainder(10, 3);
System.out.println("10除以3的余数为" + rest);
//求0.9向上取整
double ceil = Math.ceil(0.9);
System.out.println("0.9向上取整" + ceil);
//求2.49向下取整
double floor = Math.floor(2.49);
System.out.println("2.49向下取整" + floor);
//求最接近参数的整数值(若有两个满足条件的数据则取为偶数的数据)
double rint = Math.rint(3.5);
System.out.println("最接近参数的整数值" + rint);
//获得(1,1)坐标与x轴夹角度数
double atan2 = Math.atan2(1, 1);
System.out.println("坐标(1,1)的极坐标为" + atan2);
//求3的5次方
double pow = Math.pow(3, 5);
System.out.println("3的5次方" + pow);
//4舍5入
double round = Math.round(3.5);
System.out.println("3.5四舍五入为" + round);
//计算2<<5+3<<4(会抛出结果溢出异常)------- 2<<5:2乘以2的5次方 3<<4:3乘以2的4次方
int sum = Math.addExact(2 << 5, 3 << 4);
System.out.println("2<<5+3<<4=" + sum);
//计算2<<5-3<<4(会抛出结果溢出异常)
int subTract = Math.subtractExact(2 << 5, 3 << 4);
System.out.println("2<<5-3<<4=" + subTract);
//计算2<<5*3<<4
int multiply = Math.multiplyExact(2 << 5, 3 << 4);
System.out.println("2<<5*3<<4=" + multiply);
//计算2<<5加1(会跑出溢出异常)
int increment = Math.incrementExact(2 << 5);
System.out.println("2<<5+1 = " + increment);
//计算2<<5加1(会跑出溢出异常)
int decrementExact = Math.decrementExact(2 << 5);
System.out.println("2<<5-1 = " + decrementExact);
//计算2<<5的相反数
int negate = Math.negateExact(2 << 5);
System.out.println("2<<5的相反数是" + negate);
//long转int 2<<15
int intNum = Math.toIntExact(2 << 15L);
System.out.println(intNum);

//2&3 2在2进制中为10 3为11 & 表示相同位都为1则为1,其他为0 10&11 2进制第二位都为1 所以 2&3=10=2

//multiplyHigh意义未知
//计算2<<5除以2<<4(取整)
int floorDiv = Math.floorDiv(2<<5,2<<4);
System.out.println("2<<5/2<<4="+floorDiv);
//取模运算(算法与取余相同,但负数运算是计算方式不同)
int floorMod = Math.floorMod(10,3);
System.out.println("10/3的模="+floorMod);
//取-52的绝对值
int abs = Math.abs(-52);
System.out.println("-52的绝对值="+abs);
//比较2<<5和3<<4的大小
int max = Math.max(2<<5,3<<4);
System.out.println("2<<5与3<<4相比,较大的数为"+max);
//比较2<<5和3<<4的大小
int min = Math.min(2<<5,3<<4);
System.out.println("2<<5与3<<4相比,较小的数为"+min);
//计算3乘5加4
double fma = Math.fma(3,5,4);
System.out.println("3乘5加4="+fma);
}
}

执行结果

sin3.14=0.0015926529164868282
cos0=1.0
tan0.785=0.9992039901050427
arcsin1 = 1.5707963267948966
arccos = 0.0
arctan30 = 1.5374753309166493
180度角的弧度为3.141592653589793
π的角度数为180.0
以e为底指数为1的数为2.718281828459045
以e为底e的平方的对数2.0
以10为底100的对数2.0
100的平方根是10.0
27的立方根是3.0
10除以3的余数为1.0
0.9向上取整1.0
2.49向下取整2.0
最接近参数的整数值4.0
坐标(1,1)的极坐标为0.7853981633974483
3的5次方243.0
3.5四舍五入为4.0
2<<5+3<<4=112
2<<5-3<<4=16
2<<5*3<<4=3072
2<<5+1 = 65
2<<5-1 = 63
2<<5的相反数是-64
65536
2<<5/2<<4=2
10/3的模=1
-52的绝对值=52
2<<5与3<<4相比,较大的数为64
2<<5与3<<4相比,较小的数为48
3乘5加4=19.0

 

 

所有 Number 派生类 parseInt 方法格式类似如下:

static int parseInt(String s)

static int parseInt(String s, int radix)

参数

  • s -- 十进制表示的字符串。

  • radix -- 指定的基数。

返回值

  • parseInt(String s): 返回用十进制参数表示的整数值。

  • parseInt(int i): 使用指定基数的字符串参数表示的整数 (基数可以是 10, 2, 8, 或 16 等进制数) 。

实例

public class Test{
    public static void main(String args[]){
        int x =Integer.parseInt("9");
        double c = Double.parseDouble("5");
        int b = Integer.parseInt("444",16);

        System.out.println(x);
        System.out.println(c);
        System.out.println(b);
    }
}

编译以上程序,输出结果为:

9
5.0
1092