大数字运算, BigInteger

package com.ykmimi.test1;

import java.math.BigInteger;

/**
 * 大数字运算
 * @author ukyor
 *
 */
public class BigNumber {

    public static void main(String[] args) {
        //实例化一个大数字 将十进制4转换为BigInteger形式.
        BigInteger bigInstance = new BigInteger("4");
        //取该大数字加2的操作
        BigInteger bigInstanceAdd = bigInstance.add(new BigInteger("2"));
        System.out.println(bigInstance+"+2="+bigInstanceAdd);
        //取该大数字减3的操作
        BigInteger bigInstanceSubtract = bigInstance.subtract(new BigInteger("3"));
        System.out.println(bigInstance+"-3="+bigInstanceSubtract);
        //取该大数字乘以2的操作
        BigInteger bigInstanceMultiply = bigInstance.multiply(new BigInteger("2"));
        System.out.println(bigInstance+"*2="+bigInstanceMultiply);
        //取该大数字除以2的操作
        BigInteger bigInstanceDivide = bigInstance.divide(new BigInteger("2"));
        System.out.println(bigInstance+"/2="+bigInstanceDivide);
        //取除商    
        System.out.println("4/3的商为"+bigInstance.divideAndRemainder(new BigInteger("3"))[0]);
        //取余数
        System.out.println("4/3的余数为"+bigInstance.divideAndRemainder(new BigInteger("3"))[1]);
        //取该大数字的次方
        System.out.println("4的3次方为"+bigInstance.pow(3));
        //取该大数字的相反数
        System.out.println("4的相反数为"+bigInstance.negate());
    }
}

 

posted @ 2017-11-01 09:18  ukyo--君君小时候  阅读(303)  评论(0编辑  收藏  举报