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:
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.Code:DecimalFormat dfForm = new DecimalFormat("0.00");
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:
The output would be:Code:System.out.println(dfForm.format(3.14159));
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.3.14
Example:
The output of this would be:Code:float F_PI = Float.parseFloat(dfForm.format(3.14159)); System.out.println(F_PI);
The output doesn't change but what you are printing is a float value not a string.3.14
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:
Now the object formats to exactly three numbers after the decimal. So this code will output:Code:dfForm.applyPattern("0.000"); System.out.println(dfForm.format(3.14159));
Printf3.142
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:
The different formatting flags are:Code:System.out.format("PI to 4 digits is %.4f",3.14159);
%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:
This outputs the number appended with a + sign and rounded to 4 decimal places. Then a new line character is printed at the end.Code:System.out.format("%+.4f%n",3.14159);
Output:
An example using more than one formatting specifier:+3.1416
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.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);
Try running this code:
You will get an error returned that looks like this:Code:System.out.format("%+.4f is PI to four digits. %.5f is a random number formatted to 20 places.%n",Math.random()*5+1);
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)
You are piping out [good] tutorials left and right! +rep, looking forward to the next!![]()
Well done! +rep
Great tutorial! I don't know if it will let me +rep you because I recently +rep'ed you but if it doesn't let me, remind me I owe you +rep
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
I haven't kept up in the tutorial section lately. Another nice one!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks