• JAVA大数处理(BigInteger,BigDecimal)


    原文链接
    Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类。

    这两个类都在java.math.*包中,因此每次必须在开头处引用该包。

    Ⅰ基本函数:

    1.valueOf(parament);//将参数转换为制定的类型
    2.add(); //大整数相加
    3.subtract(); //相减
    4.multiply(); //相乘
    5.divide(); //相除取整
    6.remainder(); //取余
    7.pow(); //a.pow(b)=a^b
    8.gcd(); //最大公约数
    9.abs(); //绝对值
    10.negate(); //取反数
    11.mod(); //a.mod(b)=a%b=a.remainder(b);
    12.max(); min();
    13.public int comareTo();
    14.boolean equals();//是否相等
    15.//BigInteger构造函数:
    BigInteger(String val);
    //将指定字符串转换为十进制表示形式;
    BigInteger(String val,int radix);
    //将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger

    Ⅱ.基本常量:

    A=BigInteger.ONE 1
    B=BigInteger.TEN 10
    C=BigInteger.ZERO 0

    Ⅲ.基本操作——读入与输出

    用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中

    Scanner cin=new Scanner(System.in);// 读入
    while(cin.hasNext())   //等同于!=EOF
    {
       int n;
       BigInteger m;
       n=cin.nextInt(); //读入一个int;
       m=cin.BigInteger();//读入一个BigInteger;
    System.out.print(m.toString());
    }

    Ⅳ.四则运算:

    import java.util.Scanner;
    import java.math.*;
    import java.text.*;
    public class Main {
        public static void main(String args[]) {
           Scanner cin = new Scanner ( System.in );
           BigInteger a,b;
           int c;
           char op;
           String s;
    
           while( cin.hasNext() ) {
                a = cin.nextBigInteger();
                s = cin.next();
                op = s.charAt(0);
                if( op == '+') {
                    b = cin.nextBigInteger();
                    System.out.println(a.add(b));
                }
                else if( op == '-') {
                    b = cin.nextBigInteger();
                    System.out.println(a.subtract(b));
                }
                else if( op == '*') {
                    b = cin.nextBigInteger();
                    System.out.println(a.multiply(b));
                }
                else {
                    BigDecimal a1,b1,eps;
                    String s1,s2,temp;
                    s1 = a.toString();
                    a1 = new BigDecimal(s1);
                    b = cin.nextBigInteger();
                    s2 = b.toString();
                    b1 = new BigDecimal(s2);
                    c = cin.nextInt();
                    eps = a1.divide(b1,c,4);
                    //System.out.println(a + " " + b + " " + c);
                    //System.out.println(a1.doubleValue() + " " + b1.doubleValue() + " " + c);
                    System.out.print( a.divide(b) + " " + a.mod(b) + " ");
                    if( c != 0) {
                        temp = "0.";
                        for(int i = 0; i < c; i ++) temp += "0";
                        ecimalFormat gd = new DecimalFormat(temp);
                        System.out.println(gd.format(eps));
                    }
                    else System.out.println(eps);
                }
            }
        }
    }

    具体解释:

    1.valueOf(parament); 将参数转换为制定的类型
    比如 int a=3;
    BigInteger b=BigInteger.valueOf(a);
    则b=3;
    String s=”12345”;
    BigInteger c=BigInteger.valueOf(s);
    则c=12345;
    2.add(); 大整数相加
    BigInteger a=new BigInteger(“23”);
    BigInteger b=new BigInteger(“34”);
    a.add(b);
    3.subtract(); 相减
    4.multiply(); 相乘
    5.divide(); 相除取整
    6.remainder(); 取余
    7.pow(); a.pow(b)=a^b
    8.gcd(); 最大公约数
    9.abs(); 绝对值
    10.negate(); 取反数
    11.mod(); a.mod(b)=a%b=a.remainder(b);
    12.max(); min();
    13.public int comareTo();
    返回-1,0或1,分别为BigInteger在数字上小于,等于,或大于值val。
    14.boolean equals(); 是否相等
    15.BigInteger构造函数:
    一般用到以下两种:
    BigInteger(String val);
    将指定字符串转换为十进制表示形式;
    BigInteger(String val,int radix);
    将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger

    补充:
    a=a.pow(b); //a = a的b次方
    a=a.stripTrailingZeros(); //去掉后导0
    d=a.toPlainString(); //不让其变成科学计数法的表示法,变成一般的小数表示
    if(d.charAt(0)==’0’) d=d.substring(1); //与前导的字符串比较 ??
    if(s.startsWith(“0.”)) s=s.substring(1);//与前导的字符串比较 ??
    and(); // 例如a.and(b),计算a&b
    doubleValue(); //转化为double类型
    longValue(); //转化为long类型
    floatValue(); // 转化为float类型
    intValue(); //转化为int类型
    divideAndRemainder(BigInteger val)
    //返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
    boolean isProbablePrime(BigInteger n) //判断大数是否为素数
    modPow(BigInteger n, BigInteger mod) //计算this^n % mod
    nextProbablePrime(BigInteger n) //返回比大数n大的为素数的数
    not() //计算 ~this
    or(BigInteger a) //计算this|a
    xor(BigInteger val) //返回其值为 (this ^ val) 的 BigInteger。
    probablePrime(int bitLength, Random rnd)
    //返回有可能是素数的、具有指定长度的正 BigInteger。
    shiftLeft(int n) //返回其值为 (this << n) 的 BigInteger。
    shiftRight(int n) //返回其值为 (this >> n) 的 BigInteger。

  • 相关阅读:
    swift延时执行
    Swift3.0 — CocoaAsyncSocket客户端(Socket通信)
    SnapKit配置过程
    iOS App图标和启动画面尺寸
    mac升级到10.13 CocoaPods不能使用
    上传app到app store遇到 An error occurred uploading to the iTunes Store.(iTunes Store Operation Failed)的解决方法
    swift高斯模糊的自定义view
    Swift 实现部分圆角
    android 代码设置、打开wifi热点及热点的连接
    自定义android Dialog
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387020.html
Copyright © 2020-2023  润新知