Skip to content
Related Articles

Related Articles

Improve Article

BigInteger toString() Method in Java

  • Last Updated : 04 Dec, 2018

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.




    // Java program to demonstrate
    // toString(radix) method of BigInteger
      
    import java.math.BigInteger;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Creating BigInteger object
            BigInteger b1;
            b1 = new BigInteger("321456");
      
            // create radix
            int radix = 2;
      
            // apply toString(radix) method
            String b1String = b1.toString(radix);
      
            // print String
            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




    // Java program to demonstrate 
    // toString(radix) method of BigInteger
      
    import java.math.BigInteger;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Creating BigInteger object
            BigInteger b1;
            b1 = new BigInteger("34567876543");
      
            // create radix
            int radix = 8;
      
            // apply toString(radix) method
            String b1String = b1.toString(radix);
      
            // print String
            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




    // Java program to demonstrate toString(radix) method of BigInteger
      
    import java.math.BigInteger;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Creating BigInteger object
            BigInteger b1;
            b1 = new BigInteger("8765432123456");
      
            // create radix
            int radix = 16;
      
            // apply toString(radix) method
            String b1String = b1.toString(radix);
      
            // print String
            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:




    // Java program to demonstrate toString() method of BigInteger
      
    import java.math.BigInteger;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Creating 2 BigInteger objects
            BigInteger b1, b2;
            b1 = new BigInteger("35152194853456789");
      
            // apply toString() method
            String b1String = b1.toString();
      
            // print String
            System.out.println(b1String);
      
            b2 = new BigInteger("7654323234565432345676543234567");
      
            // apply toString() method
            String b2String = b2.toString();
      
            // print String
            System.out.println(b2String);
        }
    }
    Output:
    35152194853456789
    7654323234565432345676543234567
    

Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#toString(java.math.BigInteger)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :