BIgInteger和BigDecimal

 一、BIgInteger

        在Java中,由CPU原生提供的整型最大范围是64位1ong型整数。使用long型整数可以直接通过cPU指令进行计算,速度非常快。如果我们使用的整数范围超过了1og型怎么办?这
个时候,就只能用数据结构来模拟一个大整数。java.math.BigInteger就是用来表示任意大小的整数。BigInteger内部用一个int[]数组来模拟一个非常大的整数:

 BigInteger和Integer、Long一样,也是不可变类,并且也继承自Number类。因为Numbe、下定义了转换为基本类型的几个方法:


1、转换为byte:byteValue()
2、转换为short:shortValue()
3、转换为int:intValue()
4、转换为1onglongValue()
5、转换为f1oat:floatValue()
6、转换为doubledoubleValue()


因此,通过上述方法,可以把BigInteger转换成基本类型。如果BigInteger表示的范围超过了基本类型的范围,转换时将丢失高位信息,即结果不一定是准确的。如果需要准确地转换成基本类型,可以使用intValueExact()、longValueExact()等方法,在转换时如果超出范围,将直接抛
ArithmeticException异常。
BigInteger和1ong型整数运算比,BigInteger不会有范围限制,但缺点是速度比较慢。也可以把BigInteger转换成long型:

 eg1

package com.xn.Tue;
//BigInteger超大整数
//解决问题:普通整数在计算时,产生类型溢出的问题;保存超大整数;
import java.math.BigInteger;
public class Tue04 {
	public static void main(String[] args) {
		
		//类型溢出
//		int n1 Integer.MAX VALUE;
//		int n2 Integer.MAX_VALUE;
//		int sum n1 n2;
//		System.out.println(sum);
		
		//创建时,建议使用String类型保存超大整数
		BigInteger n1=new BigInteger(String.valueOf(Integer.MAX_VALUE));
		BigInteger n2=new BigInteger("76122644326335700000");
		
		//计算
		BigInteger ret1=n1.add(n2);
		BigInteger ret2=n1.subtract(n2);
		BigInteger ret3=n1.multiply(n2);
		BigInteger ret4=n1.divide(n2);
		System.out.println(ret1);
		System.out.println(ret2);
		System.out.println(ret3);
		System.out.println(ret4);
		
		//比较
		System.out.println(n1.compareTo(n2));
		System.out.println(n1.equals(n2));
	}
}

eg2

package com.xn.Tue;

import java.math.BigInteger;
import java.util.Scanner;

public class Tue05 {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		//BigInteger a=null;
//		while(in.hasNext) {
		//a=input.nextBigInteger();
//			}
		BigInteger a=new BigInteger("6");
		BigInteger b=new BigInteger("13");
		BigInteger c=new BigInteger("9");
		
		BigInteger max=(a.compareTo(b)>=0)?(a.compareTo(c)>0?a:c):(b.compareTo(c)>0?b:c);
		BigInteger min=(a.compareTo(b)<=0)?(a.compareTo(c)<0?a:c):(b.compareTo(c)<0?b:c);
		BigInteger mid=a.add(b).add(c).subtract(max).subtract(min);
		String ret=max.compareTo(min.add(mid))<0?"Yes":"No";
		System.out.println(ret);
	}
}

小结
BigInteger用于表示任意大小的整数
BigInteger是不变类,并且继承自Number父类
BigInteger转换成基本类型时可使用longValueExact()等方法保证结果准确,超出范围溢出时,会抛出ArithmeticException异常
 

 二、BigDecimal

BigDecimal和BigInteger类似,BigDecimal可以表示个任意大小且精度完全准确的浮点数。对BigDecima1做加、减、乘时,精度不会丢失,但是做除法时,存在无法除尽的情况,这时,就必须指定精度以及如何进行截断:
调用divideAndRemainder()方法时,返▣的数组包含两个BigDecimal,分别是商和余数,其中商总是整数,余数不会大于除数。我们可以利用这个方法判断两个Big0 ecima.1是否是整数倍数:

小结
BigDecimal用于表示精确的小数,常用于财务计算
比较BigDecimal的值是否相等,必须使用compareTo()而不能使用equals()
 

eg1

 

package com.xn.Tue;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Tue06 {
	public static void main(String[] args) {
		double d1=0.2;
		double d2=0.1;
		System.out.println(d1+d2);
		//乘法,减法,加法
		BigDecimal dec1=new BigDecimal("0.2");
		BigDecimal dec2=new BigDecimal("0.1");
		System.out.printf("%f+%f=%f\n",dec1,dec2,dec1.add(dec2));
		System.out.printf("%f-%f=%f\n",dec1,dec2,dec1.subtract(dec2));
		System.out.printf("%f×%f=%f\n",dec1,dec2,dec1.multiply(dec2));
		//除法:存在除不尽的情况
		//AritheticException算数逻辑异常
		//保留10位小数,并设置模式
		System.out.printf("%f÷%f=%.10f\n",dec1,dec2,dec1.divide(dec2, 10, RoundingMode.HALF_UP));
		//除法+取余0
		BigDecimal[] result=dec1.divideAndRemainder(dec2);
		System.out.println("商:"+result[0]);
		System.out.println("余数:"+result[1]);
		
	}
}

相关推荐

  1. BIgIntegerBigDecimal

    2024-01-24 20:18:02       57 阅读
  2. BigDecimal

    2024-01-24 20:18:02       41 阅读
  3. BigDecimal使用案例

    2024-01-24 20:18:02       51 阅读
  4. BigDecimal中divide方法详解

    2024-01-24 20:18:02       57 阅读
  5. C#:用 BigInteger 计算 斐波那契数列

    2024-01-24 20:18:02       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-24 20:18:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-24 20:18:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-24 20:18:02       82 阅读
  4. Python语言-面向对象

    2024-01-24 20:18:02       91 阅读

热门阅读

  1. GoLang刷题之leetcode

    2024-01-24 20:18:02       52 阅读
  2. docker 创建oceanbase数据库

    2024-01-24 20:18:02       62 阅读
  3. 网络通信(17)-C#TcpClient 和 TcpListener的详解

    2024-01-24 20:18:02       46 阅读
  4. docker常用的命令

    2024-01-24 20:18:02       46 阅读
  5. 计算机视觉有什么发展方向

    2024-01-24 20:18:02       52 阅读
  6. Spring和SpringBoot的区别

    2024-01-24 20:18:02       61 阅读
  7. 142. 环形链表 II(Python3)

    2024-01-24 20:18:02       60 阅读
  8. openssl3.2/test/certs - 025 - client intermediate ca: cca-cert

    2024-01-24 20:18:02       55 阅读
  9. 一次查找某些后缀的文件

    2024-01-24 20:18:02       48 阅读
  10. GDB调试crashdump

    2024-01-24 20:18:02       72 阅读