CHAPTER 24B — Formatted Output

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.
go to home page   go to next page        

created 07/10/2006

CHAPTER 24B — Formatted Output

This chapter discusses how to control the apperance of a number when it is converted into characters. This is usually done when a number is output to the monitor or to a file. For example, instead of

value = 13.333333333333334

you can ask for only two places right of the decimal point:

value = 13.34

This chapter is optional. Future chapters do not depend on it, so you can safely skip it. However, your programs will produce better looking output if you use number formatting.

Chapter Topics:

  • Default Locale
  • Formatting numeric output
  • DecimalFormat
  • Changing the Locale
  • Format patterns
    • 0 format character
    • . format character
    • , format character
    • # format character
  • Formats for positive and negative numbers

There is much more to formatting numbers than this chapter discusses. Sometime in the future you may wish to study the detailed documentation.

Depending on their location and language, people use different formats for numbers, dates, times, and currency. How to customize a program for the location and language where it will be used is called internationalization. This chapter only briefly discusses this.

QUESTION 1:

How would you prefer to see this number displayed:

123456789