Java 计算功率时输出错误

Java 计算功率时输出错误,java,Java,我有下面的代码 import java.util.ArrayList; import java.math.*; import java.util.Arrays; import java.util.Collections; public class Dummy { public static void main(String args[]) throws Exception { int n=12; double val=(3+Math.sqrt(5)); d

我有下面的代码

import java.util.ArrayList;
import java.math.*;
import java.util.Arrays;
import java.util.Collections;

public class Dummy {
    public static void main(String args[]) throws Exception {


    int n=12;
    double val=(3+Math.sqrt(5));

    double ne=Math.pow(val, n);

    String new2=String.valueOf(ne);
    System.out.println(ne);
    String[] new1=new2.split("\\.");

    if(new1[0].length()>3){
        new1[0]=new1[0].substring(Math.max(new1[0].length() - 4, 0));
         if(new1[0].length()<3){
                new1[0]=("0").concat(new1[0]);
            }
         else{
             new1[0]=new1[0];
         }
    }
    else if(new1[0].length()<2){
        new1[0]=("00").concat(new1[0]);
    }
    else if(new1[0].length()<1){
        new1[0]=("000").concat(new1[0]);
    }


    else if(new1[0].length()<3){
        new1[0]=("0").concat(new1[0]);
    }
    System.out.println(new1[0]);
    }

}

当我这样做的时候,我得到的结果是4.2468114719604947E8,但实际上答案是424681471.960494。请让我知道我错在哪里。

您注意到答案中的E8了吗?那么答案是正确的:

您得到的答案是正确的


4.246814719604947E8表示4.246814719604947乘以10^8。如果将小数点向右移动8位,您将看到预期的答案

这是科学记数法

如果你不想用这个符号,你可以试试

public static void main(String[] args) {
    Double d = Math.pow(3+Math.sqrt(5),12);
    System.out.println(d); //4.246814719604947E8
    System.out.println(new BigDecimal(d).toPlainString()); //424681471.960494697093963623046875
}
用这个
System.out.printlnString.格式%f,ne

没什么不对的。第一个结果是统计形式的。您需要一个格式字符串来显示4.246814719604947E8上的第二个ResultPoster onE8。检查…:只是想知道如何将我的科学结果转换为正常结果设置所有数字,我使用了'String fin=String.formatnew2,.;',但是我看不出有什么区别,而且小数是有限的或预定义的。如果我错了,请纠正我。谢谢汉克斯,伙计们,现在问题解决了汉克斯,利奥,我的问题解决了。谢谢
public static void main(String[] args) {
    Double d = Math.pow(3+Math.sqrt(5),12);
    System.out.println(d); //4.246814719604947E8
    System.out.println(new BigDecimal(d).toPlainString()); //424681471.960494697093963623046875
}