格式化双精度时正确的 printf 语法是什么?

What is the right printf syntax when formatting a double?

我卡在 printf 语句上了。谁能告诉我,格式化 doubleprintf 的正确语法是什么?

public static void main(String[] args){
    System.out.printf("%-15s %-5s" + " | " + "%5s %15s \n", "Celcius", "Fahrenheit", "Fahrenheit", "Celcius");
    for(double celcius = 40, fahrenheit = 120; celcius >= 31 && fahrenheit >= 30; celcius--, fahrenheit--){
    System.out.printf("%.1-15f%.1-5f|%.115f%.115f\n", celcius, celciusToFahrenheit(celcius), fahrenheit, fahrenheitToCelcius(fahrenheit));
    }
}

public static double celciusToFahrenheit(double celcius){
    double fahrenheit = (9.0 / 5) * (celcius + 32); 
    return fahrenheit;        
}

public static double fahrenheitToCelcius(double fahrenheit){
    double celcius = (5.0 / 9) * (fahrenheit - 32); 
    return celcius;
} 

这些是 System.out.printf() 格式规则:

%[flags][width][.precision]conversion-character

您可以找到大量 printf() here and here

的示例和解释