Applet with #'s

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.
go to previous page   go to home page   go to next page        

Answer:

Yes

Applet with #'s

Here is the fragment, now using #

double A= 0.1, B= -0.1;

DecimalFormat numform = new DecimalFormat(" ##0.###;-##0.###"); 

System.out.println( "A = " + numform.format(A) );  
System.out.println( "B = " + numform.format(B) );  

The fragment now writes:

A =  0.1
B = -0.1

Here is the applet. Play for a while.

Embedded Java applet removed: modern browsers do not support Java applets. The surrounding source code is retained for historical study.

Format patterns that contain # create strings of varying lengths, so it is hard to align numbers in columns of output.

QUESTION 16:

What does the following fragment write?

double A= 123.1, B= -1.1;

DecimalFormat numform = new DecimalFormat(" ##0.###;-##0.###"); 

System.out.println( "A = " + numform.format(A) ); 
System.out.println( "B = " + numform.format(B) );