Java 将双精度值123.0转换为123.00

Java 将双精度值123.0转换为123.00,java,division,Java,Division,我需要显示两个十进制值。双d=双123400/100;我得到的结果是d=1234.0;我使用了DecimalFormat,它给出的结果是d=1234。我需要d=1234.00的结果 尝试以下方法: double d= 123400/100.; String s = String.format("%.2f", d); System.out.println(s); 您可以像这样设置最小分数位数 setMinimumFractionDigits(2) 例如 输出 Formatted : 1234.

我需要显示两个十进制值。双d=双123400/100;我得到的结果是d=1234.0;我使用了DecimalFormat,它给出的结果是d=1234。我需要d=1234.00的结果

尝试以下方法:

double d= 123400/100.;
String s = String.format("%.2f", d);
System.out.println(s);

您可以像这样设置最小分数位数

setMinimumFractionDigits(2)
例如

输出

Formatted : 1234.00
此语句必须导致编译时错误,指出int不能转换为double

现在,您的问题的解决方案是文本格式化。使用java.text.DecimalFormat

import java.text.DecimalFormat;

public class FormatClazz {
    public static void main(String[] args) {
        Double d = 123400/100 * 1.00;
        DecimalFormat df2 = new DecimalFormat("#.00");
        System.out.println((df2.format(d)));    
    }
}

也许把你写的代码贴出来吧?DecimalFormat有一个,所以我建议你先读一读;System.out.printD=+d;1在你的帖子下使用选项添加信息,比如你在问题中使用的代码,2你说*我使用了DecimalFormat,结果是d=1234*,我在你的例子中没有看到任何DecimalFormat。双d=Double123400/100将无法编译
Formatted : 1234.00
Double d=(Double) 123400/100;
import java.text.DecimalFormat;

public class FormatClazz {
    public static void main(String[] args) {
        Double d = 123400/100 * 1.00;
        DecimalFormat df2 = new DecimalFormat("#.00");
        System.out.println((df2.format(d)));    
    }
}