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.

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/

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

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/

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?

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/