BigInteger Class offers 2 methods for toString().
- toString(int radix): The java.math.BigInteger.toString(int radix) method returns the decimal String representation of this BigInteger in given radix. Radix parameter decides on which number base (Binary, octal, hex etc) it should return the string. In case of toString() the radix is by Default 10. If the radix is outside the range of Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10.
Syntax:
public String toString(int radix)
Parameter: This method accepts a single mandatory parameter radix which is the radix of String representation.
Return Value: This method returns decimal String representation of this BigInteger in the given radix.
Examples:
Input: BigInteger1=321456 radix =2
Output: 1001110011110110000
Explanation: BigInteger1.toString(2)=1001110011110110000.
when radix is 2 then function will first convert the BigInteger
to binary form then it will return String representation of that binary number.
Input: BigInteger1=321456 radix=16
Output: 4e7b0
Explanation: BigInteger1.toString()=4e7b0.
when radix is 16 then function will first convert the BigInteger
to hexadecimal form then it will return String representation of that hexadecimal number.
Below programs illustrate toString(int radix) method of BigInteger class:
Example 1: When radix = 2. It means binary form String.
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
BigInteger b1;
b1 = new BigInteger( "321456" );
int radix = 2 ;
String b1String = b1.toString(radix);
System.out.println( "Binary String of BigInteger "
+ b1 + " is equal to " + b1String);
}
}
|
Output:
Binary String of BigInteger 321456 is equal to 1001110011110110000
Example 2: When radix = 8 It means octal form String
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
BigInteger b1;
b1 = new BigInteger( "34567876543" );
int radix = 8 ;
String b1String = b1.toString(radix);
System.out.println( "Octal String of BigInteger "
+ b1 + " is equal to " + b1String);
}
}
|
Output:
Octal String of BigInteger 34567876543 is equal to 401431767677
Example 3: When radix = 16 It means HexaDecimal form String
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
BigInteger b1;
b1 = new BigInteger( "8765432123456" );
int radix = 16 ;
String b1String = b1.toString(radix);
System.out.println( "Hexadecimal String of BigInteger "
+ b1 + " is equal to " + b1String);
}
}
|
Output:
Hexadecimal String of BigInteger 8765432123456 is equal to 7f8dc77d040
- toString(): The java.math.BigInteger.toString() method returns the decimal String representation of this BigInteger. This method is useful to convert BigInteger to String. One can apply all string operation on BigInteger after applying toString() on BigInteger.
Syntax:
public String toString()
Return Value: This method returns decimal String representation of this BigInteger.
Examples:
Input: BigInteger1=321456
Output: 321456
Explanation: BigInteger1.toString()=321456.
The answer is the String representation of BigInteger
Input: BigInteger1=59185482345
Output: 59185482345
Explanation: BigInteger1.toString()=59185482345.
The answer is the String representation of BigInteger
Below programs illustrate toString() method of BigInteger class:
Example:
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
BigInteger b1, b2;
b1 = new BigInteger( "35152194853456789" );
String b1String = b1.toString();
System.out.println(b1String);
b2 = new BigInteger( "7654323234565432345676543234567" );
String b2String = b2.toString();
System.out.println(b2String);
}
}
|
Output:
35152194853456789
7654323234565432345676543234567
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#toString(java.math.BigInteger)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Dec, 2018
Like Article
Save Article