Search code examples
javaprintingformatrounding

How do I print the remainng text?


I finally found out how to print a Double in Java with 2 decimal points. However, I now can't figure out how to keep printing text after the Double is sent out.

I tried to print something like this.

double maxTemp;
maxTemp = 5.43210;

System.out.format("\nThe highest Temperature recorded was: %.2f", maxTemp, "*C");

Then I wanted it to round the temperature to 2 decimal places and display:

The highest Temperature recorded was: 5.43*C

However it keeps cutting off the '*C' after the temperature is displayed. This is the output:

The highest Temperature recorded was: 5.43

How can I fix this?


Solution

  • Put the "*C" in the format string, not as an additional argument.

    System.out.format("%nThe highest Temperature recorded was: %.2f*C", maxTemp);
    

    Alternatively, you can use %s for a string placeholder.

    System.out.format("%nThe highest Temperature recorded was: %.2f%s", maxTemp, "*C");