This question already has answers here:
Java: Converting a double to a String
                                
                                    (4个答案)
                                
                        
                6年前关闭。
            
        

我有部分代码:

double double1 = 0.000112344;
String string1 = "0.000112344";
System.out.println(String.valueOf(double1) + " : "+string1);


结果:

1.12344E-4 : 0.000112344


问题是,如何将double1解析为将显示0.000112344的字符串?

最佳答案

double double1 = 0.000112344;
String string1 = "0.000112344";
System.out.printf("%.9f: %s%n", double1, string1);


0.000112344:0.000112344


通常,String.format("%.nf", double1)将产生一个double1的字符串表示形式,四舍五入到n小数位。

07-26 09:28