Double 大数值 精度丢失 解决方法

把String类型的且是【整型】【大数】转成Double,计数超过E15就会丢失精度。解决方法是使用BigInteger

package com;
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.DecimalFormat;  
public class TestDouble{  
    public static void main(String[] args){       
        System.out.println("以下比较理论上结果值都应该是flase才正确。出现true都是错误的");
            System.out.println("使用BigInteger");
            BigInteger no1 = new BigInteger("1000000010000000000000046");
            BigInteger no2  = new BigInteger("1000000010000000000000045");
            System.out.println("no1="+no1);
            System.out.println("no2="+no2);               
            if (no1.compareTo(no2) == 0)
            {
               System.out.println("1000000010000000000000045==1000000010000000000000046:"+true);
            }   
            else{
                System.out.println("1000000010000000000000045==1000000010000000000000046:"+false);
            }
/*             运行结果:           
            使用BigInteger
            no1=1000000010000000000000046
            no2=1000000010000000000000045
            1000000010000000000000045==1000000010000000000000046:false       */     
}

你可能感兴趣的:(Double 大数值 精度丢失 解决方法)