00018.07 数学相关API(Math、int ,Integer, BigInteger什么区别、天花板函数、以及它们的加减乘除)

系列文章目录

一、数学相关的常用类

1、java.lang.Math类

这个比较简单,但是很常用,我们直接看详细的使用
在这里插入图片描述
java.lang.Math.max(参数1,参数2)是一个静态的工具方法贴上JDK里面的源码

    /**
	* Returns the greater of two {@code int} values. That is, the
	*返回两个{代码INT}值的较大值。也就是说,
	* result is the argument closer to the value of
	*结果是更接近价值的论点
	* {@link Integer#MAX_VALUE}. If the arguments have the same value,
	*{Link整数×Max值}。如果参数具有相同的值,
	* the result is that same value.		*结果是相同的值。
	*
	*
	* @param   a   an argument.
	*PARAM A的一个起点。
	* @param   b   another argument.
	*PARAM B的另一个起点。
	* @return  the larger of {@code a} and {@code b}.
	*@返回较大的{@代码A}和{@代码B}。
	*/
    public static int max(int a, int b) {
        return (a >= b) ? a : b;
    }

很容易理解,就是两个里面取最大的

在这里插入图片描述
在这里插入图片描述

天花板函数:往上进1或者往下进1或者四舍五入,应用场景:分页中常用
在这里插入图片描述

2、java.math包

 BigInteger
 BigDecimal

什么时候会用到呢?就是你数字特别大的时候
在这里插入图片描述
那么 BigInteger怎么用呢?这样吗?好像不对劲
在这里插入图片描述

要注意,不能把它当成数字类型,你得把它当成字符串,因为字符串是多长都行的
在这里插入图片描述
那如果要求和怎么办呢,这么长
如果直接加就是拼接而不是加
在这里插入图片描述
既然是对象要实现功能,那么就需要方法

正确写法如下:
在这里插入图片描述
如果有小数,那么就用BigDecimal,需要注意的是出发会报错,因为除不尽,就无限循环了,那应该怎么办呢?
所以我们要指定,保留多少位小数
在这里插入图片描述
运行结果如下
在这里插入图片描述

二、面试题

面试题:int ,Integer, BigInteger什么区别?

int 是基本数据类型
Integer 是包装类
BigInteger 大的整数
先考虑int 需要用对象的时候用Integer ,特别大的时候用BigInteger

三、完整代码

package com.atguigu.test12;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.junit.Test;

/*
 * 数学相关的常用类:
 * 1、java.lang.Math类
 * 
 * 2、java.math包
 * BigInteger
 * BigDecimal
 * 
 * 
 * 面试题:int ,Integer, BigInteger什么区别?
 */
public class TestMath {
	@Test
	public void test4(){
		BigDecimal big1 = new BigDecimal("72.345678912345678912345678");
		BigDecimal big2 = new BigDecimal("4.2345678912345678912345678");
		
		System.out.println("和:" + big1.add(big2));
		System.out.println("减:" + big1.subtract(big2));
		System.out.println("乘:" + big1.multiply(big2));
//		System.out.println("除:" + big1.divide(big2));//如果除不尽,会报异常
		
		//divide(BigDecimal divisor, int scale, int roundingMode)
		System.out.println("除:" + big1.divide(big2, 10, BigDecimal.ROUND_CEILING));
		System.out.println("余:" + big1.remainder(big2));
	}
	
	@Test
	public void test3(){
//		long big = 12345678912345678912345678L;
		BigInteger big1 = new BigInteger("72345678912345678912345678");
		BigInteger big2 = new BigInteger("42345678912345678912345678");
		
		System.out.println("和:" + big1.add(big2));
		System.out.println("减:" + big1.subtract(big2));
		System.out.println("乘:" + big1.multiply(big2));
		System.out.println("除:" + big1.divide(big2));
		System.out.println("余:" + big1.remainder(big2));
	}
	
	
	@Test
	public void test2(){
		System.out.println(Math.ceil(2.1));//进一法
		System.out.println(Math.floor(2.1));//退一法
		System.out.println(Math.round(2.1));//四舍五入
		System.out.println();
		
		System.out.println(Math.ceil(2.6));//进一法
		System.out.println(Math.floor(2.6));//退一法
		System.out.println(Math.round(2.6));//四舍五入
		System.out.println();
		
		System.out.println(Math.ceil(-2.1));//进一法
		System.out.println(Math.floor(-2.1));//退一法
		System.out.println(Math.round(-2.1));//四舍五入
		System.out.println();
		
		System.out.println(Math.ceil(-2.6));//进一法
		System.out.println(Math.floor(-2.6));//退一法
		System.out.println(Math.round(-2.6));//四舍五入
		System.out.println();
	}
	
	@Test
	public void test1(){
		System.out.println(Math.PI);
		System.out.println(Math.sqrt(9));
		System.out.println(Math.pow(2, 8));//2的8次方
		System.out.println(Math.max(5, 9));
		System.out.println(Math.min(3, 9));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值