当前位置:   article > 正文

Java常用类库之Math_java类库math

java类库math

Math

System.out.println(Math.abs(-100));
//100
System.out.println(Math.min(100, 200));
//100
System.out.println(Math.max(100, 200));
//200

//四舍五入
System.out.println(Math.round(1.5));
//2
System.out.println(Math.round(-1.5));
//-1

//返回最接近的整数(double型)
System.out.println(Math.rint(1.5));
//2.0 
System.out.println(Math.rint(-1.5));
//-2.0

//向下取整
System.out.println(Math.floor(3.5));
//3
System.out.println(Math.floor(-3.5));
//-4

//向上取整
System.out.println(Math.ceil(3.5));
//4
System.out.println(Math.ceil(-3.5));
//-3

//int或long型数值相加
System.out.println(Math.addExact(2, 8));
//10
System.out.println(Math.addExact(2147483647, 3));
//溢出int或long会抛出异常:Exception in thread "main" java.lang.ArithmeticException: integer overflow

//返回值递减1,溢出抛异常
System.out.println(Math.decrementExact(8));
//7
System.out.println(Math.incrementExact(8));
//9

//exp()
System.out.println(Math.exp(1));
//2.718281828459045

//除法,向下取整
System.out.println(Math.floorDiv(8,3));
//2
//返回余数
System.out.println(Math.floorMod(8,3));
//2

//幂
System.out.println(Math.pow(8,2));
//64

//[0,1)的double随机数
System.out.println(Math.random());

//数学常数
System.out.println(Math.PI);
//3.141592653589793
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/77263
推荐阅读
相关标签
  

闽ICP备14008679号