Java 高精度数字类
高精度数字类
Java中的java.math包提供T两个高精度的数宇类BigInteger和BigDecimal;与原有的Long和 Double相比,它们处理更大的整数或更大的小数。值得一提的是,这两个类并没有对应的基本类 型,并且它们都是以String字符串的形式进行保存和传入。
代码如下所示
import java.math.BigDecimal;
import java.math.BigInteger;
//*BigInteger和BigDecimal示例
public class Demo {
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("1010");
System.out.println("创建BigInteger对象:" + bigInteger.toString());
bigInteger.add(new BigInteger("101"));//将返回一个新的临时对象,不会在原bigInteger对象上执 行add
System.out.println("Biglmeger对象加上 101 结果:"+ bigInteger.toString());
bigInteger = bigInteger.add (new BigInteger("101"));
System.out.println("BigInteger对象加上 101 结果:" + bigInteger.toString());
BigDecimal bigDecimal = new BigDecimal("3.14");
System.out.println("创建BigDecimalXt象:"+ bigDecimal.toString());
System.out.println("BigDecimal对象精度为:" + bigDecimal.precision());
}
}
![1606442580552466.png @E97BQQQ3AT]E9ZI{)}7)]3.png](https://www.zuixu.com/d/file/2020/11-27/1606442580552466.png)

湘公网安备:43011102000856号 

点击加载更多评论>>