根号2的小数点后100位

时间:2021-01-21 18:34:55

根号2的小数点后100位

package 根号2的小数点后100位;
import java.util.*;
import java.math.*;
public class QuickPower {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double d=Math.sqrt(2);
        String s=String.valueOf(d);
        s=s.substring(0,s.length()-1);
        BigDecimal b=new BigDecimal("2");
        //compareTo(int val) -> comparable
        //compare(int a,int b) -> comparator
        for(int i=0;i<100;i++){
            for(int j=0;j<9;j++){
                if(new BigDecimal(s+j).pow(2).compareTo(b)<0){
                    s+=j;
                }
            }
        }
        System.out.println(s);
    }

}
输出结果:
1.414213562373095012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678