當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BigInteger toString()用法及代碼示例


BigInteger類為toString()提供2種方法。

  • toString(int radix):java.math.BigInteger.toString(int radix)方法以給定的基數返回此BigInteger的十進製String表示形式。基數參數決定應返回字符串的基數(二進製,八進製,十六進製等)。對於toString(),基數默認為10。如果基數在Character.MIN_RADIX到Character.MAX_RADIX(包括這兩個字符)的範圍之外,則默認為10。

    用法:

    public String toString(int radix)

    參數:此方法接受單個強製性參數基數,該基數是String表示形式的基數。


    返回值:此方法以給定的基數返回此BigInteger的十進製String表示形式。

    例子:

    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表示八進製形式字符串

    // 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時表示十六進製十進製形式的字符串

    // 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的十進製String表示形式。此方法對於將BigInteger轉換為String很有用。在BigInteger上應用toString()之後,可以在BigInteger上應用所有字符串操作。

    用法:

    public String toString()

    返回值:此方法返回此BigInteger的十進製String表示形式。

    例子:

    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://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#toString(java.math.BigInteger)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger toString() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。