Best 10 java.math.BigInteger class Examples with tutorials
In this blog post, learn How to Convert Integer/int from/to Biginteger with examples. You can also check my previous posts on BigInteger class tutorials in java.
You can also check my previous posts on the BigInteger
class in java.
- BigInteger Class tutorials
- Convert BigInteger to/from BigDecimal
- Convert BigDecimal to/from String
- BigInteger Divide example
- BigInteger Multiplication example
- Convert BigDecimal to float
- Convert BigDecimal to double
- Rounding bigdecimal to 2 decimal places
- Check BigDecimal contains Zero or not
- Convert BigInteger to/from ByteArray
BigInteger Examples
BigInteger is a Java class defined in java.math package. BigInteger uses in arbitrary precession arithmetic calculations where the result value of this calculation is out of the range of values of all primitive numeric types.
How to Convert BigInteger to binary String?
BigInteger has toString(radix) method which takes radix parameter. radix value must be between Characters.MIN_RADIX(=2) and Character.MAX_RADIX(=36), radix is out of this range, it defaults to radix=10
public String toString(int radix) Binary String is a string that represents values in one and zero and represents in 2 base numeric values, Radix value must be passed as 2.
System.out.println(new BigInteger("1").toString(2));
System.out.println(new BigInteger("5").toString(2));
System.out.println(new BigInteger("12").toString(2));
System.out.println(new BigInteger("131").toString(2));
Output is
1
101
1100
10000011
Convert BigInteger to Octal String?
The octal string is represented in 8 base numeric values. BigInteger.toString(radix) method takes radix value 8 for Octal String conversion. Radix value must be passed as 2
System.out.println(new BigInteger("1").toString(8));
System.out.println(new BigInteger("5").toString(8));
System.out.println(new BigInteger("12").toString(8));
System.out.println(new BigInteger("64").toString(8));
Output is
1
5
14
100
Convert BigInteger to Hexadecimal Number?
Hexadecimal is represented in 16 base numeric values. BigInteger.toString(radix) method takes radix value 16 for Hexadecimal Number Conversion. Radix value must be passed as 2.
System.out.println(new BigInteger("11").toString(16));
System.out.println(new BigInteger("51").toString(16));
System.out.println(new BigInteger("112").toString(16));
System.out.println(new BigInteger("164").toString(16));
Output is
b
33
70
a4
BigInteger Pow() method
example.
BigInteger
class pow()
method, returns a big integer value which is this power exponent. the exponent is an integer Syntax is
public BigInteger pow(int exponent)
Here is an example for java.math.BigInteger.pow() method
BigInteger bigInteger=new BigInteger("5");
System.out.println(bigInteger.pow(1));
System.out.println(bigInteger.pow(3));
System.out.println(bigInteger.pow(5));
Output is
5
125
3125
BigInteger multiply() method example
This class multiply() method does the multiplication of big integer values and returns big integer objects.
public BigInteger multiply(BigInteger val)
Here is an example for java.math.BigInteger.multiply() method
BigInteger bigInteger=new BigInteger("5");
BigInteger bigInteger1=new BigInteger("6");
System.out.println(bigInteger.multiply(bigInteger1));
The output of the above code is
30
Divide BigInteger Examples
This class divide() method does divide big integer values with this and returns a big integer value.
public BigInteger divide(BigInteger val) {
Following is an code example for java.math.BigInteger.divide() method
BigInteger bigInteger1 = new BigInteger("87945697851122");
BigInteger bigInteger2 = BigInteger.valueOf(587955);
System.out.println(bigInteger1.divide(bigInteger2));
output is
149578960
BigInteger CompareTo() method Examples
It has CompareTo() method which compares two BigIntegers and returns an integer - 0,-1,1 0 - Both values are equal 1 - called object value is more than compared value -1 - called object value is less than compared value.
public int compareTo(BigInteger val)
Following is an code example for java.math.BigInteger.compareTo() method
BigInteger bigInteger=new BigInteger("5");
System.out.println(bigInteger.compareTo(new BigInteger("5")));
System.out.println(bigInteger.compareTo(new BigInteger("12")));
System.out.println(bigInteger.compareTo(new BigInteger("2")));
Output is
0
-1
1