Numbers

The printf and format Methods

The java.io package includes a PrintStream class that has two formatting methods that you can use to replace print and println. These methods, format and printf, are equivalent to one another.

Run The Application

Right click to the NumberDemo class; select Run As -> Java Application.

Output

The Decimal Format Class

You can use the java.text.DecimalFormat class to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.DecimalFormat offers a great deal of flexibility in the formatting of numbers, but it can make your code more complex.

Run The Application

Right click to the DecimalFormatDemo class; select Run As -> Java Application.

Output

Formatting currency using BigDecimal

The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.

Run The Application

Right click to the BigDecimalDemo class; select Run As -> Java Application.

Output