Java BigInteger类| toString()方法与示例

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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值