JAVA核心技术(总结)——大数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MMMMMMMW/article/details/80501992

BigInteger :整数

BigInteger i = in.nextBigInteger();

BIgDecimal : 浮点数

BigDecimal f = in.nextBigDecimal();

valyeOf : 将普通字符转化为大数

BigInteger a = BigInteger.valueOf(100);

add:相加

BigInteger c = a.add(b);//c = a + b

multiply:相乘

BigInteger d = a.multiply(b);//d = a * b

substact:相减

igInteger e = a.subtract(b);// e = a - b;

divide:相除

BigInteger g = b.divide(a);//g = b / a;

mod:取余

BigInteger m = a.mod(b);//m = a % b;

compareTo:比较

int compare1 = a.compareTo(a);//a等于a,结果为0
int compare2 = a.compareTo(b);//a小于b,结果为负
int compare3 = b.compareTo(a);//b大于a,结果为正

相关实验代码如下:

import java.math.BigInteger;
import java.util.*;
import java.io.*;
import java.math.*;

public class BigNumber
{
    public static void main(String args[])
    {
    	Scanner in = new Scanner(System.in);
    	BigInteger i = in.nextBigInteger();
    	BigDecimal f = in.nextBigDecimal();
        BigInteger a = BigInteger.valueOf(100);
        BigInteger b = BigInteger.valueOf(200);
        BigInteger c = a.add(b);//c = a + b
        BigInteger d = a.multiply(b);//d = a * b
        BigInteger e = a.subtract(b);// e = a - b;
        BigInteger g = b.divide(a);//g = b / a;
        BigInteger m = a.mod(b);//m = a % b;
        int compare1 = a.compareTo(a);//a等于a,结果为0
        int compare2 = a.compareTo(b);//a小于b,结果为负
        int compare3 = b.compareTo(a);//b大于a,结果为正
        System.out.println(i);
        System.out.println(f);
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
        System.out.println(g);
        System.out.println(m);
        System.out.println(compare1);
        System.out.println(compare2);
        System.out.println(compare3);
    }
}


展开阅读全文

没有更多推荐了,返回首页