当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。