与数学相关的类

Math

所属的包:java.lang

Java 的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。

Math 的方法都被定义为 static 形式,通过 Math 类可以在主函数中直接调用。

通常的方法

方法描述
E abs(E)返回指定参数的绝对值(E:int,long,float,double)
double ceil(double)向上取整
double floor(double)向下取整
double rint(double)取临近的整数(如果两边距离一样,则返回偶数)
int round()返回四舍五入的整数
E max(E, E)取两个数中较大的数(E:int,long,float,double)
E min(E, E)取两个数中较小的数(E:int,long,float,double)
double pow(double, double)将第一个参数的值返回到第二个参数的幂
double sqrt(double)获取给定参数的平方根
double random()产生一个[0.0-1.0)的随机数
public class Test{
	publlic static void main(String[] args){
		System.out.println(Math.ceil(1.4));//1.0
        System.out.println(Math.floor(1.4));//1.0
        System.out.println(Math.rint(1.4));//1.0
        System.out.println(Math.round(1.4));//1.0
        System.out.println(Math.sqrt(16));//4.0
	}
}

实例:

public class Test{
	public static void main(Sting[] args){
		//产生一个0-9的随机整数
		int value = (int)(Math.random() * 10);
		System.out.println(value);
	}
}

Random

所属的包:java.util

没有任何继承关系,默认继承Object类。
所有的方法都需要使用new对象调用。

构造方法:
  Random random = new Random();
常用的方法:

方法描述
int nextInt()随机产生一个int取值范围的整数
int nextInt(int bound)随机产生一个[0-bounf)的整数
float nextFloat()随机产生一个[0.0-1.0)的值
boolean nextBoolean()随机产生一个boolean的值

UUID

所属的包:java.util

没有任何继承关系,默认继承Object类
虽然有构造方法,但是一般不会创建对象

public class Test{
	public static void main(String[] args){
		UUID uuid = UUID.randomUUID();
        System.out.println(uuid.toString());//数据库表格主键 primary key
        产生一个32位的随机元素 每一个位置是一个16进制的数字
	}
}

BigInteger

所属的包:java.math
继承Number类
提供的构造方法都是带参数的,通常利用带String参数的构造方法创建这个类的对象
BigInteger类可以描述比long类型大的数据,可以解决数据溢出的问题,
常用方法:做四则运算
add() subtract() multiply() divide()

//设计一个方法 用来计算给定数字的阶乘
public BigInteger factorial(int num){
     BigInteger result = new BigInteger("1");
     for(int i = 1 ;i <= num ;i++){
         result = result.multiply(new BigInteger(i + ""));
     }
     return result;
 }
public class Test{
	public static void main(String[] args){
		BigInteger big1 = new BigInteger("123");
		BigInteger big2 = new BigInteger("456");
		big1.add(big2);
		big1.subtract(big2);
		big1.multiply(big2);
		big1.divide(big2);	

		//创建对象 调用计算阶乘的方法
        TestMath tm = new TestMath();
        BigInteger bi = tm.factorial(10);
        System.out.println(bi.toString());	
	}
}

BigDecima

所属的包:java.math
继承Number类
提供的构造方法都是带参数的,通常利用带String参数的构造方法创建这个类的对象
BigDecimal 类则是针对很大的小数的处理类精度比较高,可以处理金融类业务,如果对计算的数据要求高精度时,必须使用BigDecimal类
常用方法:做四则运算
add() subtract() multiply() divide()

public class Test{
	public static void main(String[] args){
		BigDecimal bd = new BigDecimal("123.456");
        //小数点之后保留两位  按照向下取整的方式进行截取
        bd = bd.setScale(2,BigDecimal.ROUND_DOWN);
        System.out.println(bd);//123.45
	}
}

DecimaFormat

所属的包:java.text
通过String参数的构造方法创建一个格式化对象

public class Test{
	public static void main(String[] args){
		DecimalFormat df = new DecimalFormat("000.###");//0表示必须有 #表示可有可无  四舍五入原则
        String value = df.format(123.456789);
        System.out.println(value);//123.457
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值