+ Reply to Thread
Results 1 to 5 of 5

Thread: Formatting Strings

  1. #1
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Formatting Strings

    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:

    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:

    Code:
    float F_PI = Float.parseFloat(dfForm.format(3.14159));
    System.out.println(F_PI);
    The output of this would be:

    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:

    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:

    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:

    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:
    +3.1416
    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)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Formatting Strings

    You are piping out [good] tutorials left and right! +rep, looking forward to the next!

  4. #3
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Formatting Strings

    Well done! +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Re: Formatting Strings

    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!

  6. #5
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Formatting Strings

    I haven't kept up in the tutorial section lately. Another nice one!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. formatting the output
    By jackson6612 in forum C and C++
    Replies: 3
    Last Post: 10-18-2011, 10:03 PM
  2. USB formatting
    By TeenChristian in forum Computer Software/OS
    Replies: 1
    Last Post: 08-21-2010, 09:51 AM
  3. Formatting Help
    By rocketmanjp in forum Java Help
    Replies: 3
    Last Post: 04-12-2010, 11:55 PM
  4. formatting
    By kumamako in forum C and C++
    Replies: 5
    Last Post: 07-22-2009, 10:28 AM
  5. Paragraph Formatting in C++, help!
    By biciclub in forum C and C++
    Replies: 4
    Last Post: 07-02-2009, 06:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts