Negative Values

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. However, the size of the groups should all be the same.

Negative Values

When a number is negative, a minus sign is output in front of all the characters called for by the format pattern. If there are spaces on the left of the format pattern, they will be output, with the minus sign to the left of them. This is usually an error. See the last example.

value of double format pattern output string
-123.456
"000.000"
"-123.456"
-123.456
"000.0"  
"-123.5"
-89.008
"0.00"    
"-89.01"
-89.008
"0."       
"-89."
-1.1
"0000.0000"     
-0001.1000"
-89.008
"  0.0"     
"-  89.0"

The minus sign does not replace the leading zero. Here is the applet (yet again) for you to play with:

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

 

QUESTION 12:

What does the following fragment write?

DecimalFormat numform = new DecimalFormat("-0.00"); 
System.out.println( "Num = " + numform.format(-924.56) );