Java 常用方法实例 Math类

abs()

返回参数的绝对值。

public class 绝对值 {
  public static void main(String[] args) {
    Integer a = -8;
    double d = -100.1;
    float f = -90.1;
    
    System.out.println(Math.abs(a));
    System.out.println(Math.abs(d));
    System.out.println(Math.abs(f));
  }
}

执行结果

8
100.0
90.0

ceil()

返回大于等于( >= )给定参数的最小整数,类型为双精度浮点型。

public class 大于等于{
    public static void main(String args[]){
        double d = 100.675;
        float f = -90;    
 
        System.out.println(Math.ceil(d));
        System.out.println(Math.ceil(f)); 
    }
}

执行结果

101.0
-90.0

floor()

返回小于等于(<=)给定参数的最大整数 。

public class 小于等于 {
  public static void main(String[] args) {
    double d = 100.675;
    float f = -90;

    System.out.println(Math.floor(d));
    System.out.println(Math.floor(f));
  }
}

执行结果

100.0
-90.0

rint()

返回与参数最接近的整数。返回类型为double。

public class 最接近整数 {
  public static void main(String[] args) {
    double d = 100.675;
    double e = 100.500;
    double f = 100.200;
    
    System.out.println(Math.rint(d));
    System.out.println(Math.rint(e));
    System.out.println(Math.rint(f));
  }
}

执行结果

101.0
100.0
100.0

round()

四舍五入

public class 四舍五入 {
  public static void main(String[] args) {
    double d = 100.675123456;
    double e = 100.500;
    float f = 100;
    float g = 90f;
    
    System.out.println(Math.round(d));
    System.out.println(Math.round(e));
    System.out.println(Math.round(f));
    System.out.println(Math.round(g));
  }
}

执行结果

101
101
100
90

min()

返回两个参数中的最小值。

public class 最小值 {
  public static void main(String[] args) {
    System.out.println(Math.min(12.123, 12.456));
    System.out.println(Math.min(23.12, 23.0));
  }
}

执行结果

12.123
23.0

max()

返回两个参数中的最大值。

public class 最大值 {
  public static void main(String[] args) {
    System.out.println(Math.max(12.123, 18.456));
    System.out.println(Math.max(23.12, 23.0));
  }
}

执行结果

18.456
23.12

exp()

返回自然数底数e的参数次方。

public class e次方 {
  public static void main(String[] args) {
    double x = 11.635;
    double y = 2.76;
    System.out.printf("e 的值为 %.4f%n", Math.E);
    System.out.printf("exp(%.3f) 为 %.3f%n", x, Math.exp(y));
  }
}

执行结果

e 的值为 2.7183
exp(11.635)15.800

log()

返回参数的自然数底数的对数值。

public class 对数 {
  public static void main(String[] args) {
    double x = 11.635;
    double y = 2.76;
    System.out.printf("e 的值为 %.4f%n", Math.E);
    System.out.printf("log(%.3f) 为 %.3f%n", x, Math.log(x));
  }
}

执行结果

e 的值为 2.7183
log(11.635)2.454

pow()

返回第一个参数的第二个参数次方。

public class x的y次方 {
  public static void main(String[] args) {
    double x = 11.635;
    double y = 2.76;

    System.out.printf("e 的值为 %.4f%n", Math.E);
    System.out.printf("pow(%.3f, %.3f) 为 %.3f%n", x, y, Math.pow(x, y));
  }
}

执行结果

e 的值为 2.7183
pow(11.635, 2.760)874.008

sqrt()

求参数的算术平方根。

public class 平方根 {
  public static void main(String[] args) {
    double x = 11.635;
    double y = 2.76;
    System.out.printf("e 的值为 %.4f%n", Math.E);
    System.out.printf("sqrt(%.3f) 为 %.3f%n", x, Math.sqrt(x));
  }
}

执行结果

e 的值为 2.7183
sqrt(11.635)3.411

sin()

求指定double类型参数的正弦值。

public class sin {
  public static void main(String[] args) {
    double degrees = 45.0;
    double radians = Math.toRadians(degrees);
    System.out.format("pi 的值为 %.4f%n", Math.PI);
    System.out.format("%.1f 度的正弦值为 %.4f%n", degrees, Math.sin(radians));
  }
}

执行结果

pi 的值为 3.1416
45.0 度的正弦值为 0.7071

cos()

求指定double类型参数的余弦值。

public class cos {
  public static void main(String[] args) {
    double degrees = 45.0;
    double radians = Math.toRadians(degrees);
    System.out.format("pi 的值为 %.4f%n", Math.PI);
    System.out.format("%.1f 度的余弦值为 %.4f%n", degrees, Math.cos(radians));
  }
}

执行结果

pi 的值为 3.1416
45.0 度的余弦值为 0.7071

tan()

求指定double类型参数的正切值。

public class tan {
  public static void main(String[] args) {
    double degrees = 45.0;
    double radians = Math.toRadians(degrees);
    System.out.format("pi 的值为 %.4f%n", Math.PI);
    System.out.format("%.1f 度的正切值是 %.4f%n", degrees, Math.tan(radians));
  }
}

执行结果

pi 的值为 3.1416
45.0 度的正切值是 1.0000

asin()

求指定double类型参数的反正弦值。

public class asin {
  public static void main(String[] args) {
    double degrees = 45.0;
    double radians = Math.toRadians(degrees);
    System.out.format("pi 的值为 %.4f%n", Math.PI);
    System.out.format("%.4f 的反正弦值为 %.4f 度 %n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians))));
  }
}

执行结果

pi 的值为 3.1416
0.7071 的反正弦值为 45.0000

acos()

求指定double类型参数的反余弦值。

public class acos {
  public static void main(String[] args) {
    double degrees = 45.0;
    double radians = Math.toRadians(degrees);
    System.out.format("pi 的值为 %.4f%n", Math.PI);
    System.out.format("%.4f 的反余弦值为 %.4f 度 %n", Math.cos(radians), Math.toDegrees(Math.acos(Math.sin(radians))));
  }
}

执行结果

pi 的值为 3.1416
0.7071 的反余弦值为 45.0000

atan()

求指定double类型参数的反正切值。

public class atan {
  public static void main(String[] args) {
    double degrees = 45.0;
    double radians = Math.toRadians(degrees);
    System.out.format("pi 的值为 %.4f%n", Math.PI);
    System.out.format("%.4f 的反正切值 %.4f 度 %n", Math.cos(radians), Math.toDegrees(Math.atan(Math.sin(radians))));
  }
}

执行结果

pi 的值为 3.1416
0.7071 的反正切值 35.2644

atan2()

将笛卡尔坐标转换为极坐标,并返回极坐标的角度值。

public class atan2 {
  public static void main(String[] args) {
    double x = 45.0;
    double y = 30.0;
    System.out.println( Math.atan2(x, y) );
  }
}

执行结果

0.982793723247329

toDegrees()

将参数转化为角度。

public class 弧度转角度 {
  public static void main(String[] args) {
    double x = 45.0;
    double y = 30.0;
    System.out.println( Math.toDegrees(x) );
    System.out.println( Math.toDegrees(y) );
  }
}

执行结果

2578.3100780887044
1718.8733853924696

toRadians()

将角度转换为弧度。

public class 角度转弧度 {
  public static void main(String[] args) {
    double x = 45.0;
    double y = 30.0;
    System.out.println( Math.toRadians(x) );
    System.out.println( Math.toRadians(y) );
  }
}

执行结果

0.7853981633974483
0.5235987755982988

random()

返回一个随机数。

public class 随机数 {
  public static void main(String[] args) {
    //1-100随机整数
    System.out.println( Math.round(Math.random()*100) );
    //0-1随机小数
    System.out.println( Math.random() );
  }
}

执行结果

61
0.2622526999347229
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天吼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值