BigInteger类toString()方法 (BigInteger Class toString() method)
Syntax:
句法:
public String toString();
public String toString(int radix's);
toString() method is available in java.math package.
toString()方法在java.math包中可用。
toString() method is used to represent a decimal string of this BigInteger.
toString()方法用于表示此BigInteger的十进制字符串。
toString(int radix's) method is used to represent a string of this BigInteger in the given radix's value.
toString(int radix's)方法用于以给定的基数值表示此BigInteger的字符串。
These methods don't throw an exception at the time of representing a string.
这些方法在表示字符串时不会引发异常。
These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.
这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, toString()
在第一种情况下, toString()
- None
In the second case, toString(int radix's)
在第二种情况下, toString(int radix's)
- int radix's – represents the radix used to denote this BigInteger as a String.
- int radix's-表示用于将BigInteger表示为String的基数。
Return value:
返回值:
In both the cases, the return type of the method is String,
在这两种情况下,方法的返回类型均为String 。
In the first case, it returns the decimal string denotation of this BigInteger.
在第一种情况下,它返回此BigInteger的十进制字符串表示形式。
In the second case, it returns string denotation of this BigInteger in the given radix.
在第二种情况下,它以给定的基数返回此BigInteger的字符串表示形式。
Example:
例:
// Java program to demonstrate the example
// of toString() method of BigInteger
import java.math.*;
public class ToStringOfBI {
public static void main(String args[]) {
// Initializes a variables str
String str = "10";
int radix1 = 10;
int radix2 = 2;
// Initializes a BigInteger object
BigInteger b_int = new BigInteger(str);
// Here, this method toString() method is
// used to represent this BigInteger b_int value as
// a String
String str_conv = b_int.toString();
System.out.println("b_int.toString(): " + str_conv);
// Here, this method toString(int) method is
// used to represent this BigInteger b_int value as
// a String in the given radix "10" i.e. 10
// means decimal representation of b_int
str_conv = b_int.toString(radix1);
System.out.println("b_int.toString(radix1): " + str_conv);
// Here, this method toString(int) method is
// used to represent this BigInteger b_int value as
// a String in the given radix "2" i.e. 2
// means binary representation of b_int
str_conv = b_int.toString(radix2);
System.out.println("b_int.toString(radix2): " + str_conv);
}
}
Output
输出量
b_int.toString(): 10
b_int.toString(radix1): 10
b_int.toString(radix2): 1010
翻译自: https://www.includehelp.com/java/biginteger-tostring-method-with-example.aspx