Math类的常用方法
最近在刷LeetCode的时候,发现有很多和算术相关的题目,可以使用Math类处理,所以小结一下,加深印象。
1.取近似值
1.1四舍五入
int/long Math.round(float/double d) (推荐使用)
描述:如果入参是float,返回int类型,如果入参是double,返回long类型。
public static void main(String[] args) {
float f1 = 1.34f;
double f2 = 1.5;
double f3 = 2.5;
System.out.println(Math.round(f1));
System.out.println(Math.round(f2));
System.out.println(Math.round(f3));
}
输出:
1
2
3
double Math.rint(double d);
描述:入参必须是double类型(比double类型范围小的数值类型都可以),返回类型是double类型。
缺点:当处理小数部分刚好是.5的数值时,结果会取和当前值最接近的偶数,所以不推荐使用这种方法进行四舍五入的操作。
public static void main(String[] args) {
float f1 = 1.34f;
float f2 = 1.5f;
float f3 = 2.5f;
System.out.println(Math.rint(f1));
System.out.println(Math.rint(f2));
System.out.println(Math.rint(f3));
}
输出:
1.0
2.0
2.0
1.2 向上、向下取整
double java.lang.Math.ceil(double a):向上取整。只要小数位包含有效值,取比当前值大的最小整数作为结果,否则结果不变。
double java.lang.Math.floor(double a):向下取整。只要小数位包含有效值,取比当前值小的最大整数作为结果,否则结果不变。
public static void main(String[] args) {
//向上、向下取整
System.out.println(Math.ceil(-2.1));
System.out.println(Math.floor(2.34));
}
输出:
-2.0
2.0
2. 求绝对值、最大值、最小值
double java.lang.Math.abs(double a):求绝对值。
double java.lang.Math.max(double a, double b):求最大值,a和b相同,则随意返回一个。
double java.lang.Math.min(double a, double b):求最小值,a和b相同,则随意返回一个。
public static void main(String[] args) {
//求最大值、最小值、绝对值
System.out.println(Math.max(2.11, 2.12));
System.out.println(Math.min(2.11, 2.12));
System.out.println(Math.abs(-2.11));
}
输出:
2.12
2.11
2.11
3. 求n次方及n次方根
double java.lang.Math.sqrt(double a):求平方根。
double java.lang.Math.cbrt(double a):求立方根。
推荐使用:
double java.lang.Math.pow(double a, double b)
描述:入参为所有基本数值类型,返回值double类型。底数是a,指数是b。该方法既可以用来计算n次方,也可以用来计算n次方根,只要把指数换成1/n的方式即可。
public static void main(String[] args) {
//求n次方,2是底数,3是指数
System.out.println(Math.pow(2, 3));
//求n次方根
System.out.println(Math.sqrt(8));//求平方根:square root
System.out.println(Math.cbrt(8));//求立方根:cube root
System.out.println(Math.pow(16,1/(double)4));//求N次方根
}
输出:
8.0
2.8284271247461903
2.0
2.0
4. 获取一定范围内的随机数
double java.lang.Math.random():返回[0,1)范围内的随机数(左闭右开区间)。
//获取[1,100]内的随机整数
System.out.println((int)(Math.random()*100+1));
5. 三角函数
5.1 角度和弧度之间的转换
double java.lang.Math.toRadians(double angdeg):度数转弧度。
double java.lang.Math.toDegrees(double angrad):弧度转度数。
5.2 基本三角函数的计算
double java.lang.Math.sin(double a):正弦函数。a表示弧度值。
double java.lang.Math.cos(double a):余弦函数。a表示弧度值。
double java.lang.Math.tan(double a):正切函数。a表示弧度值。
public static void main(String[] args) {
//角度和弧度之间的转换
System.out.println(Math.toRadians(30));
System.out.println(Math.toDegrees(Math.PI/6));
//基本三角函数计算
System.out.println(Math.sin(Math.PI/6));
System.out.println(Math.cos(Math.PI/3));
System.out.println(Math.tan(Math.PI/4));
}
输出:
0.5235987755982988
29.999999999999996
0.49999999999999994
0.5000000000000001
0.9999999999999999
附加:数值的格式化问题
java.text.DecimalFormat
public static void main(String[] args) {
//数字的保留小数问题
//使用#的特点:对于整数部分,不做处理,对于小数部分,只有超出时做四舍五入
DecimalFormat decimalFormmat = new DecimalFormat("#.##");
System.out.println(decimalFormmat.format(3));
System.out.println(decimalFormmat.format(3.1));
System.out.println(decimalFormmat.format(3.123));
System.out.println(decimalFormmat.format(12.3));
System.out.println(decimalFormmat.format(123.456));
System.out.println("-------------------------");
//使用0的特点:对于整数部分,超出部分不作处理,不足部分,用0补齐。对于小数部分,超出部分,四舍五入,不足部分,0补齐
DecimalFormat decimalFormmat1 = new DecimalFormat("00.00");
System.out.println(decimalFormmat1.format(3));
System.out.println(decimalFormmat1.format(3.1));
System.out.println(decimalFormmat1.format(3.123));
System.out.println(decimalFormmat1.format(12.3));
System.out.println(decimalFormmat1.format(123.456));
System.out.println("-------------------------");
//组合使用,不修改整数部分,只对小数部分保留2位小数,不足以0补齐
DecimalFormat decimalFormmat2 = new DecimalFormat("#.00");
System.out.println(decimalFormmat2.format(3));
System.out.println(decimalFormmat2.format(3.1));
System.out.println(decimalFormmat2.format(3.123));
System.out.println(decimalFormmat2.format(12.3));
System.out.println(decimalFormmat2.format(123.456));
}
输出:
3
3.1
3.12
12.3
123.46
-------------------------
03.00
03.10
03.12
12.30
123.46
-------------------------
3.00
3.10
3.12
12.30
123.46
暂时写到这里啦!
------------------2020/06/06更新-----------------------------
关于生成随机数问题
1.使用Math.random();
2.使用Random类。
3.使用SecureRandom类。
public static void main(String[] args) {
//使用Math.random类,生成伪随机数,它的底层实际上和Random类相同,产生的数字看起来随机,但实际上不是真随机
long start = System.currentTimeMillis();
for(int a = 0;a<10;a++){
//Math.random(),返回double类型的值,[0,1)
System.out.print((int)(Math.random()*10));
if(a==9){
System.out.println();
}
}
long end = System.currentTimeMillis();
System.out.println("耗时:"+(end-start));
//使用Random类生成伪随机数
start = System.currentTimeMillis();
Random random = new Random();
for(int a = 0;a<10;a++){
//.nextInt(n),表示产生[0,n)之间的随机整数
System.out.print(random.nextInt(10));
if(a==9){
System.out.println();
}
}
end = System.currentTimeMillis();
System.out.println("耗时:"+(end-start));
//SecureRandom,速度慢,它是真正的随机数
start = System.currentTimeMillis();
SecureRandom secureRandom = new SecureRandom();
for(int a = 0;a<10;a++){
//.nextInt(n),表示产生[0,n)之间的随机整数
System.out.print(secureRandom.nextInt(10));
if(a==9){
System.out.println();
}
}
end = System.currentTimeMillis();
System.out.println("耗时:"+(end-start));
}
输出:
7445348100
耗时:1
8106150363
耗时:0
0619654538
耗时:578
------------------2021/09/01更新-----------------------------
最近使用DecimalFormat进行格式化操作时,使用格式如下:
import java.math.*;
import java.text.*;
public class DecimalFormatTest
{
public static void main(String[] args)
{
BigDecimal bigDecimal = new BigDecimal("0.123");
//保留2位小数,这种方式0没了
DecimalFormat decimalFormat1 = new DecimalFormat("#.00");
System.out.println(decimalFormat1.format(bigDecimal));
//这种格式化方式有0,推荐使用
DecimalFormat decimalFormat2 = new DecimalFormat("#0.00");
System.out.println(decimalFormat2.format(bigDecimal));
}
}
输出:
.12
0.12
结论就是:推荐使用new DecimalFormat("#0.00")这种格式来格式化BigDecimal类型的数据。