如何在同一个printf中做间距和两位小数

How to do spacing and two decimal places in the same printf

如何使用 printf 在同一个 printf 中格式化间距和两位小数?

我可以使用格式化间距;

System.out.printf("%-20s%-35s%-35s\n", "name : "+shape.getShapeName(),"shape area : "+shape.calculateArea(), "shape perimeter : "+shape.calculatePerimiter());

我可以使用格式化两位小数;

System.out.printf("Name : "+shape.getShapeName()+" shape area : %.2f shape perimeter : %.2f \n",shape.calculateArea(),shape.calculatePerimiter());

但是,我无法在同一个 printf 中同时执行这两项操作。

这是最小的可重现代码;

public class WhosebugCode {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String shapeName = "Circle";
        double shapeArea = 2.2222222222;
        double shapePerimeter = 5.555555;
        
        System.out.printf("%-20s%-35s%-35s\n", "name : "+shapeName,"shape area : "+shapeArea, "shape perimeter : "+shapePerimeter);
        
        System.out.printf("Name : "+shapeName+" shape area : %.2f shape perimeter : %.2f \n",shapeArea,shapePerimeter);
    }

}
public class WhosebugCode {



/**
* @param args
*/
public static void main(String[] args) {
String shapeName = "Circle";
double shapeArea = 2.2222222222;
double shapePerimeter = 5.555555;

System.out.printf("%-20s%-35s%-35s\n", "name : "+shapeName,"shape area : "+shapeArea, "shape perimeter : "+shapePerimeter);

System.out.printf("Name : "+shapeName+" shape area : %.2f shape perimeter : %.2f \n",shapeArea,shapePerimeter);


//spacing and two decimal places in the same printf
System.out.printf( "Name : "+shapeName+"%6s"+" shape area : %.2f %6s shape perimeter : %.2f \n","",shapeArea,"",shapePerimeter);
}
}