Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: String formatting

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default String formatting

    I'm not very knowledgeable of String formatting, so I was hoping someone could answer what I hope will be an easy question:

    I am using String.format to format Strings into some specified pattern. How would I write the format String for currency?

    For example, I'd like to convert:
    1005.1000032
    Into:
    $1,005.10

    I attempted something as simple as:
    "$%.2s"
    If I input: 105.1000032
    It converts it to: $10

    And if I input 1005.1000032
    The test program never actually starts up. I assume some sort of infinite loop or something going on, but I don't care right now since my format is incorrect anyway.

    What should my format String be? Keep in mind that I am NOT converting it to a number type before formatting it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: String formatting

    Quote Originally Posted by aussiemcgr View Post
    Keep in mind that I am NOT converting it to a number type before formatting it.
    Why not?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String formatting

    See the Formattable class for ideas on how to have custom formatting of an object's contents.

    --- Update ---

    BTW Formattable is an interface not class
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: String formatting

    I was not wanting to convert it to a number first because I am creating a generalized formatting class for a custom swing table class I've made, so I didn't want to restrict the formatting to only numbers. At the moment, I don't know if I'll need to do any formatting with non-numbers, but I'd rather not create an infrastructure which will require me to do a lot of refactoring if that condition does present itself.
    Basically, I just have this simple class (with what will be a handful of public constants as "common" formats):
    public class TableColumnFormatter {
     
    	public static final String MONEY_FORMAT = "$%(,.2s"; 
     
    	private String format;
     
    	public TableColumnFormatter(String form) {
    		format = form;
    	}
     
    	public String formatCell(String content) {
    		return String.format(format,content);
    	}
    }
    I map that class to a column in my custom swing table and it will automatically format the cells in that column when I add objects to the table. The custom swing table has a hell of a lot more extra stuff with it, but this is just one of the things I'm wanting to include.

    Although the more I look at examples online, the more it seems like the only way to do this in any simple fashion is with a number...
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String formatting

    Are going to use only what the String class's format() method supports?
    Or are you trying to extend the formatting rules with your own definitions?
    If you are not going to extend what format() supports, what is the purpose of the new class?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: String formatting

    There is the very likely possibly down the line that I will attempt to include cell rendering information within the formatting class. However, I haven't really scoped that out yet. For example, the TableColumnFormatter class would contain information for both content formatting and/or rendering flags for a specific column. The TableColumnFormatter then gets mapped to a column so I can resolve formatting and rendering information without having to map both individually. I haven't designed any solid plans for it yet, but I imagine I would set it up so the rendering flags would resolved at table initialization, while the cell formatting is resolved each time an object is added to the table.
    Keep in mind that this extremely in the frameworking and designing stage. I have a handful of design plans which I am attempting to implement in a basic fashion to see how realistic my plans are. And then I have other plans which are in an "implement when I need to" state.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Formatting this number.
    By MR bruto in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 10th, 2013, 02:08 AM
  2. formatting issues help
    By Zomby in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2012, 02:28 PM
  3. String Formatting on JOptionPane.showMessageDialog
    By Onur in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 23rd, 2011, 08:39 AM
  4. Formatting Output
    By mael331 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 14th, 2011, 06:41 PM