该文章Github地址:https://github.com/AntonyCheng/java-notes
在此介绍一下作者开源的SpringBoot项目初始化模板(Github仓库地址:https://github.com/AntonyCheng/spring-boot-init-template & CSDN文章地址:https://blog.csdn.net/AntonyCheng/article/details/136555245),该模板集成了最常见的开发组件,同时基于修改配置文件实现组件的装载,除了这些,模板中还有非常丰富的整合示例,同时单体架构也非常适合SpringBoot框架入门,如果觉得有意义或者有帮助,欢迎Star & Issues & PR!
上一章:由浅到深认识Java语言(20):包装类
33.Math类
定义:
Math 类包含用于执行基本数学运算的方法,例如初等指数,对数,平方根和三角函数等;
用法:
Math.方法名(参数);
Math常用方法
abs(int a)
返回一个数的绝对值;
package top.sharehome.Bag;
public class Demo {
public static void main(String[] args) {
int i = -10;
int abs = Math.abs(i);
System.out.println(abs);
}
}
打印效果如下:
ceil(double a)
向上取整;
package top.sharehome.Bag;
public class Demo {
public static void main(String[] args) {
double i = 10.23;
double ceil = Math.ceil(i);
System.out.println(ceil);
}
}
打印效果如下:
floor(double a)
向下取整;
package top.sharehome.Bag;
public class Demo {
public static void main(String[] args) {
double i = 10.23;
double floor = Math.floor(i);
System.out.println(floor);
}
}
打印效果如下:
max(int a,int b)
求最大值(多态);
package top.sharehome.Bag;
public class Demo {
public static void main(String[] args) {
int a = 10;
float b = 10.2f;
float max = Math.max(a, b);
System.out.println(max);
}
}
打印效果如下:
pow(double a,double b)
求某个数的几次幂(ab);
package top.sharehome.Bag;
public class Demo {
public static void main(String[] args) {
double a = 10;
double b = 2;
double pow1 = Math.pow(a, b);
double pow2 = Math.pow(b, a);
System.out.println(pow1+" "+pow2);
}
}
打印效果如下:
random()
获取一个大于等于 0 且小于 1 的随机数;这里是伪随机数
package top.sharehome.Bag;
public class Demo {
public static void main(String[] args) {
double random = Math.random();
System.out.println(random);
}
}
打印效果如下:
round(float a)
对小数四舍五入;
package top.sharehome.Bag;
public class Demo {
public static void main(String[] args) {
double a = 9.2;
double b = 9.9;
long round1 = Math.round(a);
long round2 = Math.round(b);
System.out.println(round1+" "+round2);
}
}
打印效果如下:
sqrt(double a)
计算平方根;
package top.sharehome.Bag;
public class Demo {
public static void main(String[] args) {
double a = 64;
double sqrt = Math.sqrt(a);
System.out.println(sqrt);
}
}
打印效果如下: