+ Reply to Thread
Results 1 to 5 of 5

Thread: Formatting Strings

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Formatting Strings

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

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,652
    Blog Entries
    57

    Re: Formatting Strings

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

  4. #4
    Code Warrior Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana's Avatar
    Join Date
    Oct 2008
    Posts
    4,055
    Blog Entries
    6

    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
    My Site | Questions and Answers | Ask Me: Termana | Last Tutorial: Ajax innerHTML
    If you can keep your head while all around you are losing theirs, you probably have a CD writer on your desktop

  5. #5
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,883
    Blog Entries
    25

    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. User Input: Strings and Numbers [C]
    By dcs in forum C Tutorials
    Replies: 2
    Last Post: 08-30-2009, 06:00 AM
  2. Sorting C++ Strings
    By whitey6993 in forum C Tutorials
    Replies: 6
    Last Post: 06-09-2009, 02:49 AM
  3. C Strings, C++ Strings?
    By telboon in forum C and C++
    Replies: 20
    Last Post: 11-20-2008, 08:16 PM
  4. Strings (CrashCourse)
    By LogicKills in forum C Tutorials
    Replies: 3
    Last Post: 09-17-2008, 07:28 PM
  5. Strings
    By clookid in forum PHP Tutorials
    Replies: 2
    Last Post: 01-13-2007, 03:23 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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