我想在打印 float 时实现以下格式。
Consider following example →
- 5.55555555 → 5.5555
- 9.0 → 9
对于第一部分,我使用了
System.out.printf("%.4f",5.5555556f);
当我将 9.0 传递到上面的同一代码段时,它会输出为 9.0000
。请帮我解决这个问题。 ✌️
最佳答案
import java.text.DecimalFormat;
public class DecimalFormatting {
public static void main(String[] args) {
double answer = 5.0;
double answer1 = 5.55555555;
DecimalFormat df = new DecimalFormat("###.####");
System.out.println(df.format(answer));
System.out.println(df.format(answer1));
}
}
关于java float 格式说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61422590/