对双打使用字符串格式
Using String format for doubles
是否可以仅使用字符串格式来格式化值 0.41 到 00.41 的 double?换句话说,是否可以留下零填充 doubles ?我想要的结果应该显示为 xx.xx
不幸的是我没能找到简单的方法,虽然我确信有一个简单的解决方案...我已经尝试 %02.2f 但它不起作用...
感谢您的帮助!
试试这个
public class DoubleFormat {
public static void main(String[] args) {
float f = 0.41f;
String s = Float.toString(f);
System.out.println("0"+s); //Or
System.out.printf("%05.2f\n", f);
}
}
是否可以仅使用字符串格式来格式化值 0.41 到 00.41 的 double?换句话说,是否可以留下零填充 doubles ?我想要的结果应该显示为 xx.xx
不幸的是我没能找到简单的方法,虽然我确信有一个简单的解决方案...我已经尝试 %02.2f 但它不起作用...
感谢您的帮助!
试试这个
public class DoubleFormat {
public static void main(String[] args) {
float f = 0.41f;
String s = Float.toString(f);
System.out.println("0"+s); //Or
System.out.printf("%05.2f\n", f);
}
}