BigInteger and BigDecimal In Java

BigInteger and BigDecimal In Java

Introduction:

In this quick guide, we’ll introduce you to BigInteger and BigDecimal classes in Java.

BigInteger:

java.math.BigInteger is an immutable class which is used when we need to store and operate on very large integer values.

Consider writing a program involving typical mathematical calculations which probably might end up with a very large integral outcome. For such cases, we must choose to work with BigInteger to avoid data overflow issues.

Instantiation & Basic Usage:

One of the common ways to instantiate a BigInteger would make use of below constructor:

new BigInteger(String value); 

 Let’s look at some common usages with an example:

int   -2147483648~2147483647  

long long:-9223372036854775808  ~  9223372036854775807

import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {

        BigInteger b1 = new BigInteger("34353646766666667");
        BigInteger b2 = new BigInteger("4363775485686776070780607080");

//Common Operations

        BigInteger b3 = null;

        b3 = b1.add(b2);// " + "
        b3 = b2.subtract(b1); //" - "
        b3 = b1.multiply(b2);//" * "
        b3 = b2.divide(b1);//" / "
        b3 = b1.gcd(b2);//""
        b3 = b1.pow(10); //Note that pow accepts an integer argument
    }
}
	Scanner scanner = new Scanner(System.in);
	BigInteger x = scanner.nextBigInteger();//可以接收int类型
	BigInteger y = scanner.nextBigInteger();//也可以利用BigInteger.valueOf()将其他类型转换为BigInteger类型
	
	BigInteger gcd = x.gcd(y);	//BigInteger自带的求最大公约数的方法

	//BigInteger.valueOf()使用演示
	int a = 10;
	BigInteger b = BigInteger.valueOf(a);	

To avoid the outcome being lost, we have assigned it to another variable b3.

Please refer the JavaDoc in case you wish to explore it further.

BigDecimal:

java.math.BigDecimal class is also an immutable class used to store and work with very large decimal numbers,  the ones which can’t fit in basic primitive options available.

Instantiation & Basic Usage:

Common constructors that we are most likely to use includes:

BigDecimal(String value)
BigDecimal(String value, MathContext mc)

BigDecimal(double value)
BigDecimal(double value, MathContext mc)

We need to keep two things in mind while working with decimals:

  1. Precision: number of digits in a number.
  2. Scale: number of digits to the right side of a decimal point in a number.

So for number = 3456.34, precision = 6 and scale = 2.

java.math.MathContext  helps us define precision and RoundingMode to be used to maintain that precision.

Basic Operations:

Let’s look at some basic operations this class supports with the help of an example:

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

public class Main {
    public static void main(String[] args) {

        BigDecimal d1 = new BigDecimal(34346364.55);
        BigDecimal d2 = new BigDecimal(3534646.4336);

        BigDecimal d3 = null;

        d3 = d1.add(d2);
        d3 = d1.subtract(d2);
        d3 = d1.multiply(d2);
        d3 = d1.divide(d2, 2, RoundingMode.CEILING); // here, scale = 2
        d3 = d1.divide(d2, 2, RoundingMode.FLOOR);
    }
}

We’ll suggest you exploring other methods of this class by referring to its JavaDoc.

Conclusion:

This was a very short guide introducing us to BigInteger and BigDecimal classes in Java.  We might find them helpful when dealing with large numbers.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值