Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244791
  • 博文数量: 164
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1129
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 21:55
文章分类

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Java

2015-05-01 16:56:32




点击(此处)折叠或打开

  1. /*
  2.  * BigInteger:可以让超过Integer范围内的数据进行运算
  3.  *
  4.  * 构造方法:
  5.  * BigInteger(String val)
  6.  *
  7.  *
  8.  * public BigInteger add(BigInteger val):加
  9.  * public BigInteger subtract(BigInteger val):减
  10.  * public BigInteger multiply(BigInteger val):乘
  11.  * public BigInteger divide(BigInteger val):除
  12.  * public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
  13.  */
  14. public class BigIntegerDemo {
  15.     public static void main(String[] args) {
  16.         
  17.         // 这几个测试,是为了简单超过int范围内,Integer就不能再表示,所以就更谈不上计算了。
  18.         // Integer i = new Integer(100);
  19.         // System.out.println(i);
  20.         // // System.out.println(Integer.MAX_VALUE);
  21.         // Integer ii = new Integer("2147483647");
  22.         // System.out.println(ii);
  23.         // // NumberFormatException
  24.         // Integer iii = new Integer("2147483648");
  25.         // System.out.println(iii);

  26.         // 通过大整数来创建对象
  27.         BigInteger bi = new BigInteger("2147483648");
  28.         System.out.println("bi:" + bi);
  29.         
  30.         
  31.         BigInteger bi1 = new BigInteger("100");
  32.         BigInteger bi2 = new BigInteger("50");

  33.         // public BigInteger add(BigInteger val):加
  34.         System.out.println("add:" + bi1.add(bi2));
  35.         // public BigInteger subtract(BigInteger val):加
  36.         System.out.println("subtract:" + bi1.subtract(bi2));
  37.         // public BigInteger multiply(BigInteger val):加
  38.         System.out.println("multiply:" + bi1.multiply(bi2));
  39.         // public BigInteger divide(BigInteger val):加
  40.         System.out.println("divide:" + bi1.divide(bi2));

  41.         // public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
  42.         BigInteger[] bis = bi1.divideAndRemainder(bi2);
  43.         System.out.println("商:" + bis[0]);
  44.         System.out.println("余数:" + bis[1]);
  45.     }
  46. }

阅读(579) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~