Jump to content

Formatting Strings

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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:

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:

System.out.println(dfForm.format(3.14159));

The output would be:

Quote

3.14

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:

float F_PI = Float.parseFloat(dfForm.format(3.14159));
System.out.println(F_PI);

The output of this would be:

Quote

3.14

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:

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:

Quote

3.142

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:

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:

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:

Quote

+3.1416

An example using more than one formatting specifier:

Quote

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:

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)

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You are piping out [good] tutorials left and right! +rep, looking forward to the next! :)

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Well done! +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
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!


#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I haven't kept up in the tutorial section lately. Another nice one!