JAVA中的大数字BigInteger和BigDecimal
2013-09-24 20:30
316 查看
BigInteger 和 BigDecimal 是在java.math包中已有的类,前者表示整数,后者表示浮点数 。
为什么用大数字?
1) BigInteger:支持任意精度的整数,可以精确地表示任意大小的整数值,同时在运算过程中不会丢失任何信息。
2) BigInteger:可以精确地表示任意精度的小数,同时在运算过程中不会丢失任何信息。
注意:不能直接用符号如+、-来使用大数字,例如:
主要有以下方法可以使用:
BigInteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other)
BigInteger divide(BigInteger other)
BigInteger mod(BigInteger other)
int compareTo(BigInteger other)
static BigInteger valueOf(long x)
本文出自 “意大利小乳猪” 博客,请务必保留此出处http://lovesisi.blog.51cto.com/6414433/1301207
为什么用大数字?
1) BigInteger:支持任意精度的整数,可以精确地表示任意大小的整数值,同时在运算过程中不会丢失任何信息。
2) BigInteger:可以精确地表示任意精度的小数,同时在运算过程中不会丢失任何信息。
注意:不能直接用符号如+、-来使用大数字,例如:
import java.math.BigInteger; public class Main { public static void main(String[] args) { int a = 231, b = 423, c = 1393; BigInteger x, y, z, ans; x = BigInteger.valueOf(a); y = BigInteger.valueOf(b); z = BigInteger.valueOf(c); ans = x.add(y); //加运算 System.out.print(ans+" "); ans = z.divide(y); //除运算 System.out.print(ans+" "); ans = x.mod(z); //模运算 System.out.print(ans+" "); if (ans.compareTo(x) == 0) System.out.println("1"); } }运算结果:654 3 231 1
主要有以下方法可以使用:
BigInteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other)
BigInteger divide(BigInteger other)
BigInteger mod(BigInteger other)
int compareTo(BigInteger other)
static BigInteger valueOf(long x)
本文出自 “意大利小乳猪” 博客,请务必保留此出处http://lovesisi.blog.51cto.com/6414433/1301207
相关文章推荐
- java中处理高精度大数字的类 BigDecimal, BigInteger
- Java的大数操作分为BigInteger和BigDecimal
- java大数字操作:BigInteger,BigDecimal(浮点型)
- java数字精确计算的解决方案---BigDecimal
- java中如何使用BigDecimal使得Double类型保留两位有效数字
- Java使用BigDecimal保留double、float运算精度、保留指定位数有效数字、四舍五入
- Java 大数类BigInteger和BigDecimal的基本函数
- java的数字精确计算问题-BigDecimal
- Java中精确的数字计算类BigDecimal
- Java使用BigDecimal保留double、float运算精度、保留指定位数有效数字、四舍五入
- Java中精确的数字计算类BigDecimal
- Java中精确的数字计算类BigDecimal
- Java模块 -- BigDecimal 高精度数字计算
- JAVA大数类 BigInteger和BigDecimal用法
- Java核心API -- 5(包装类、BigDecimal、BigInteger)
- Java有N个数字的int类型数组,如何获取数量最多并且数字最大的数以及数量
- 使用java将一个int数字进行翻转,例如32输出2
- Java常用类库--Math、Random、NumberFormat、DecimalFormat、BigInteger、BigDecimal
- BigDecimal 除法运算出现 java.lang.ArithmeticException
- java 将一个字符串里连续两个及以上数字反转