1
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." );
    }

}

How do I print the trailing zeros of the variable named area of type double without having to break from the print statement? I used System.out.printf("%.2f",area); to have the output display 6.60 rather than 6.6

The output looks like:

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.

But I would like the output to display as:

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.
1
  • "...stackoverflow prevents the text from printing the output as it actually does in my code editor program" See the Markdown help page, specifically the first section about code and preformatted text. Short answer: Start every line with 4 spaces to get a preformatted code (or output) block. Commented Sep 13, 2015 at 6:04

2 Answers 2

1

You can use String.format() method.

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");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This is exactly what I was looking for.
@Boognish That was good. If you are ok, please mark my answer as accepted stackoverflow.com/help/someone-answers.
0

Use like this -

System.out.print( String.format( "%.2f", area));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.