Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让输出显示尾随零?_Java_Formatting_Printf_String Formatting - Fatal编程技术网

Java 如何让输出显示尾随零?

Java 如何让输出显示尾随零?,java,formatting,printf,string-formatting,Java,Formatting,Printf,String Formatting,如何在不中断print语句的情况下打印double类型的area变量的尾随零?我使用了System.out.printf(“%.2f”,面积)使输出显示为6.60而不是6.6 输出如下所示: public class StackOF01 { public static void main(String[] args) { String schoolTown = "Minneapolis"; String schoolState = "Minnes

如何在不中断print语句的情况下打印double类型的area变量的尾随零?我使用了
System.out.printf(“%.2f”,面积)使输出显示为6.60而不是6.6

输出如下所示:

public class StackOF01 {

    public static void main(String[] args) {
        String schoolTown = "Minneapolis";    
        String schoolState = "Minnesota";    
        String county01 = "Ramsey";      
        String county02 = "Hennepin";   
        long censusYear = 2010;        
        long censusPopulation = 382599;  
        String censusBureau = "Census Bureau"; 
        double area = 6.60;  
        double land = 6.52;  
        double water = 0.08;  
        String river = "Mississippi";  
        String campus = "University of Minnesota-Twin Cities";  
        int sections = 2; 

        System.out.println(schoolTown + ", " + schoolState + ":\n" );
        System.out.println(schoolTown + " is a city in " + county01 + " and " + county02 + " counties in the U.S. State "
                + "of " + schoolState + ". " + "As of the census of \n" + censusYear + ", " + "there were " + censusPopulation + " people.\n" );
        System.out.println("According to the United States " + censusBureau + "," + " the school has a total area of "); 
        System.out.printf("%.2f",area);
        System.out.println(" square miles, of which,\n" + land + " square miles is land and " + water + " square miles is water.\n");
        System.out.println(schoolTown + " lies on the banks of the " + river + " River, The South Fork of the " + river + " River ");
        System.out.println("runs through the city, dividing the campus of the " + campus + " into " + sections + " sections." );
    }

}
但我希望输出显示为:

Minneapolis, Minnesota:

Minneapolis is a city in Ramsey and Hennepin counties in the U.S. State of Minnesota. As of the census of 
2010, there were 382599 people.

According to the United States Census Bureau, the school has a total area of
6.60 square miles, of which,
6.52 square miles is land and 0.08 square miles is water.

Minneapolis lies on the banks of the Mississippi River, The South Fork of the Mississippi River |
runs through the city, dividing the campus of the University of Minnesota-Twin Cities into 2 sections.
明尼苏达州明尼阿波利斯市: 明尼阿波利斯是美国明尼苏达州拉姆齐县和亨内宾县的一座城市。截至2000年人口普查 2010年,共有382599人。 根据美国人口普查局的数据,学校总面积为6.60平方英里,其中, 6.52平方英里是陆地,0.08平方英里是水。 明尼阿波利斯位于密西西比河的南岸,密西西比河的南岸 穿过城市,把明尼苏达大学双城分校的校园分成2个部分。 像这样使用-

Minneapolis, Minnesota: Minneapolis is a city in Ramsey and Hennepin counties in the U.S. State of Minnesota. As of the census of 2010, there were 382599 people. According to the United States Census Bureau, the school has a total area of 6.60 square miles, of which, 6.52 square miles is land and 0.08 square miles is water. Minneapolis lies on the banks of the Mississippi River, The South Fork of the Mississippi River runs through the city, dividing the campus of the University of Minnesota-Twin Cities into 2 sections. 你可以使用这个方法


谢谢这正是我要找的。@Boognish,很好。如果您同意,请将我的答案标记为已接受。“…stackoverflow阻止文本像在我的代码编辑器程序中那样打印输出”请参阅本页,特别是关于的第一节。简短回答:每行用4个空格开始,得到一个预先格式化的代码(或输出)块。
System.out.print( String.format( "%.2f", area));
System.out.println("According to the United States " + censusBureau
    + "," + " the school has a total area of "
    + String.format("%.2f", area) + " square miles, of which,\n"
    + land + " square miles is land and " + water
    + " square miles is water.\n");