在 Java 中解析和格式化一个大整数到八进制
首先,创建两个 BigInteger 对象并设置值。
BigInteger one, two;
one = new BigInteger("99");现在,将 BigInteger 对象“two”解析为八进制形式。
two = new BigInteger("1100", 8);
String str = two.toString(8);以上我们使用了以下构造函数。此处,radix 设置为 8,表示八进制。对于 BigInteger 构造函数和 toString() 方法都是如此。
BigInteger(String val, int radix)
此构造函数用于将指定基数中 BigInteger 的字符串表示形式转换为 BigInteger。
以下是一个示例 -
示例
import java.math.*;
public class Demo {
public static void main(String[] args) {
BigInteger one, two;
one = new BigInteger("99");
// parsing BigInteger object "two" into Octal
two = new BigInteger("1100", 8);
String str = two.toString(8);
System.out.println("Result (BigInteger) : " +one);
System.out.println("Result after parsing : " +str);
}
}输出
Result (BigInteger) : 99 Result after parsing : 1100
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP