📜  Java中的 BigInteger toString() 方法

📅  最后修改于: 2022-05-13 01:54:58.016000             🧑  作者: Mango

Java中的 BigInteger toString() 方法

BigInteger 类为 toString() 提供了 2 种方法。

  • toString(int radix) : Java.math.BigInteger.toString(int radix) 方法以给定的基数返回此 BigInteger 的十进制字符串表示形式。 Radix 参数决定它应该返回字符串的基数(二进制、八进制、十六进制等)。在 toString()的情况下,基数默认为 10。如果基数超出字符.MIN_RADIX 到字符.MAX_RADIX 的范围(含),它将默认为 10。

    句法:

    public String toString(int radix)

    参数:此方法接受一个强制参数基数,它是字符串表示的基数。

    返回值:此方法以给定的基数返回此 BigInteger 的十进制字符串表示形式。

    例子:

    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.
    

    下面的程序说明了 BigInteger 类的 toString(int radix) 方法:

    例 1:当 radix = 2 时,表示二进制形式的字符串。

    // 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);
        }
    }
    
    输出:
    Binary String of BigInteger 321456 is equal to 1001110011110110000
    

    示例 2:当 radix = 8 表示八进制形式 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);
        }
    }
    
    输出:
    Octal String of BigInteger 34567876543 is equal to 401431767677
    

    例 3:当 radix = 16 表示 HexaDecimal 形式的 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);
        }
    }
    
    输出:
    Hexadecimal String of BigInteger 8765432123456 is equal to 7f8dc77d040
    

  • toString() : Java.math.BigInteger.toString() 方法返回这个 BigInteger 的十进制字符串表示。此方法对于将 BigInteger 转换为 String 很有用。在对 BigInteger 应用 toString() 之后,可以对 BigInteger 应用所有字符串操作。

    句法:

    public String toString()

    返回值:此方法返回此 BigInteger 的十进制字符串表示形式。

    例子:

    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
    

    下面的程序说明了 BigInteger 类的 toString() 方法:

    例子:

    // 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);
        }
    }
    
    输出:
    35152194853456789
    7654323234565432345676543234567
    

参考: https: Java Java)