java 处理高精度问题

在用C或者C++处理大数时 会非常麻烦,要手动模拟,但是在JAVA中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,理论上能够表示无线大的数,只要计算机内存足够大。
这两个类都在java.math. * 包中,因此每次必须在开头处引用该包。 import java.math. * () ; import java.util.* ; —读取用的包。

Ⅰ基本函数:
1.valueOf(parament); 将参数转换为制定的类型
String s=”12345”;
BigInteger c=BigInteger.valueOf(s);
则c=12345;
将int转化为BigInteger
int i=5;
BigInteger c= BigInteger.valueOf( i ) ;

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 compareTo();
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

Ⅲ.输入操作

import java.util.*;
import java.math.*;
class Main {
    public static void main(String[] args){
    Scanner cin = new Scanner (System.in);
    while(cin.hasNext()){  // !=EOF 
        int m;        m = cin.nextInt();
        double n;     n=  cin.nextDouble();
        BigInteger k; k=  cin.nextBigteger();
        String s;     s=  cin.next();

        System.out.println(m+"  "+n); // println 换行 print 不换行
    }

    }
}

Ⅳ.运用 四则预算:

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main  
{  
    public static void main(String args[])  
    {
        Scanner sca = new Scanner(System.in);
        BigInteger a,b;
        char op;//运算符
        String s;
        while(sca.hasNext()){
            a = sca.nextBigInteger();
            s = sca.next();
            op = s.charAt(0); // s的第一个字符
            b = sca.nextBigInteger();
            if(op == '+')
                System.out.println(a.add(b));
            else if(op == '-')
                System.out.println(a.subtract(b));
            else if(op == '*')
                System.out.println(a.multiply(b));
            else{
                int c;        //为小数设置精度
                BigDecimal ta,tb,eps;
                String sa,sb,temp;
                sa = a.toString();
                sb = b.toString();
                ta = new BigDecimal(sa);// ******
                tb = new BigDecimal(sb);
                c = sca.nextInt();
                eps = t1.divide(t2,c,4); //c是精度,4是规则,标准的四舍五入类型的。
                //System.out.println(a + " " + b + " " + c);  
                //System.out.println(t1.doubleValue() + " " + t2.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";
                    DecimalFormat gd = new DecimalFormat(temp); // 新创建一个c精度的类型。
                    System.out.println(gd.format(eps));
                }
                else
                    System.out.println(eps);
            }
        }
    }
}

注意 这个除法的时候,这个代码下,如果输入5 / 3 0
输出就是2 。为什么呢? 5 / 3 1 的时候为1.7 ,前面也说了,是四舍五入的,所以就是 后一位进1,所以就是2。

浮点数的格式控制

import java.util.Scanner;
import java.math.BigInteger;
import java.math.BigDecimal;

import java.text.DecimalFormat;  // 格式控制的包
class Test {
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        BigDecimal a= cin.nextBigDecimal();//输入 1.2222222
        DecimalFormat gs = new DecimalFormat("0.000");
        System.out.println(gs.format(a)); // 输出 1.222
         BigDecimal a=cin.nextBigDecimal();// 输入1.9
        DecimalFormat gs = new DecimalFormat("0");
        System.out.println(gs.format(a));//输出2
    }
}

divide
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied.

public DecimalFormat(String pattern)
Creates a DecimalFormat using the given pattern and the symbols for the default locale. This is a convenient way to obtain a DecimalFormat when internationalization is not the main concern.
To obtain standard formats for a given locale, use the factory methods on NumberFormat such as getNumberInstance. These factories will return the most appropriate sub-class of NumberFormat for a given locale.*

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值