Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading