Java:Math 数学计算类

作者 : admin 本文共805个字,预计阅读时间需要3分钟 发布时间: 2024-06-16 共1人阅读

文章目录

  • 一、Math
  • 二、常用API

一、Math

Math 类包含执行基本数值运算的方法,例如基本指数、对数、平方根和三角函数。


二、常用API

public class Main {
public static void main(String[] args) {
// 1、abs:求参数的绝对值
System.out.println(Math.abs(-10)); // 10
// 2、cell:向上取整
System.out.println(Math.ceil(1.2)); // 2.0
// 3、floor:向下取整
System.out.println(Math.floor(1.2)); // 1.0
// 4、round:四舍五入取整
System.out.println(Math.round(1.4)); // 1
System.out.println(Math.round(1.5)); // 2
// 5、max:求两个数中最大的数
System.out.println(Math.max(1, 2)); // 2
// 6、min:求两个数中最小的数
System.out.println(Math.min(1, 2)); // 1
// 7、pow:求a的b次幂的值
System.out.println(Math.pow(2, 3)); // 8.0
// 8、sqrt:求平方根
System.out.println(Math.sqrt(4)); // 2.0
// 9、cbrt:求立方根
System.out.println(Math.cbrt(8)); // 2.0
// 10、random:返回一个 0.0 到 0.9 范围内的随机小数值
System.out.println(Math.random()); // 0.11312200876860057
// 需求:求一个1 - 100之间的随机数
System.out.println(Math.floor(Math.random() * 100) + 1); // 43.0
}
}
本站无任何商业行为
个人在线分享 » Java:Math 数学计算类
E-->