Thousands Separators

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        
DecimalFormat numform = new DecimalFormat("000,000.00");
System.out.println( "Num = " + numform.format(98765.432) );

Answer:

Num = 098,765.43

Thousands Separators

The format pattern to the left of the decimal separator can include commas to show thousands-separators. Even in locales where comma is not the proper thousands separator, the format string uses comma. The output string will use the correct separator for the default locale. (There are methods that change this behavior, not covered here.)

There must be no thousands-separators in the characters on the right of the decimal point.

Here is the applet again. Play a bit with patterns that include thousands-separators.

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

Try putting thousands separators on the right of the decimal point. The program will throw an exception. (The applet writes an error message when this happens.)

QUESTION 11:

Try a format pattern like 0,0,0,0.0 where groups of digits other than three are separated. Does this work?