Text Formatting
Often times you have a string that you would like to format. The idea for formatting a string is so that it displays in a neater format. I.e. you want a number displayed with 3 decimal places.
The java.text package contains several classes, The class I am going to look at is DecimalFormat.
DecimalFormat
This class is used for formatting floating point numbers. The constructor takes a string describing how you want to format numbers.
Example:
Code:
DecimalFormat dfForm = new DecimalFormat("0.00");
This example creates an object that formats floating point numbers to exactly two numbers after the decimal and at least one number before the decimal.
The DecimalFormat object contains a format method to format integers. Say you want to format the number 3.14159 to 2 decimal places you would write:
Code:
System.out.println(dfForm.format(3.14159));
The output would be:
Note that the format method returns a string. If you want to format it and keep it as a float you would have to convert it back to a float using a wrapper class.
Example:
Code:
float F_PI = Float.parseFloat(dfForm.format(3.14159));
System.out.println(F_PI);
The output of this would be:
The output doesn't change but what you are printing is a float value not a string.
If you later decide you want to format numbers to 3 decimal places not two, you don't have to create a new DecimalFormat object. You can use the applyPattern method to change the pattern.
Example:
Code:
dfForm.applyPattern("0.000");
System.out.println(dfForm.format(3.14159));
Now the object formats to exactly three numbers after the decimal. So this code will output:
Printf
Java has it's onw version of the C style printf function. The printf method takes at least two parameters. The first parameter is a string of text and special formatting modifiers. These formatting modifiers indicate how the variable should be formatted. Then a variable number of arguments follow.
Example:
Code:
System.out.format("PI to 4 digits is %.4f",3.14159);
The different formatting flags are:
%d which is to be used to print a decimal integer.
%f which is to be used to print a floating point number
%n used to print a new line. Note that you cannot use \n.
Examples of these formatting operations:
Code:
System.out.format("%+.4f%n",3.14159);
This outputs the number appended with a + sign and rounded to 4 decimal places. Then a new line character is printed at the end.
Output:
An example using more than one formatting specifier:
System.out.format("%+.4f is PI to four digits. %.5f is a random number formatted to 20 places.%n",3.14159,Math.random()*5+1);
For each format specifier that you specify you must include a variable at the end to be formatted. So if you specify two formatting strings, you must specify two variables to be formatted.
Try running this code:
Code:
System.out.format("%+.4f is PI to four digits. %.5f is a random number formatted to 20 places.%n",Math.random()*5+1);
You will get an error returned that looks like this:
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.5f'
This means that you didn't specify something to be formatted for the second specifier. Note that for %n you don't have to specify anything as it is automatically replaced with \n.
These are very useful functions to get to know. A good research area would be to research the java.text package and see just what you can do. You can do a lot with this package.
Enjoy,
James
Resources:
Learning Java - Chapter 5 : Tech
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum