Java language question: formatting numbers

First, I know next to nothing about Java language. So pardon my base ignorance. I just got assigned to a project at work where we have a programmer generating Java code for an application with an Oracle back end. The problem is that he doesn’t know how to (simply) format numeric input/output display so that commas are used for every 1000s place. In other languages this might be called input/output masking. In other words if I have someone input the number:

45678

Then I want the display to show:

45,678

Likewise 657849030 should display as 657,849,030.

Now in most languages/applications with which I am familiar this is a relatively to ridiculously easy task. But I am told that the only way this can be done in Java is to take the input as string, read it, parse it, and write it back out with commas. Likewise numbers being written to display would have to be parsed into string. This seems like a very common thing to have to do and it would seem logical that there is a simple way to do this.

Does this guy just not know what he’s doing, or am I missing something here?

Thanks for all responses, even sarcastic ones making fun of my lamentable lack of WebSkillz[sup]TM[/sup]!

The short, non-technical answer: tell him to look at the NumberFormat class.

Technical answer: I don’t have my Java books here at home, but you should be able to do something like this:

NumberFormat nf = NumberFormat.getInstance(Locale.US);

Then you use that object like this to convert from number to string:

String s = nf.format(yourNumber);

And to go backwards, it’s like this:

yourNumber = nf.parse(s);

That should take care of the commas automatically.

SmackFu is right. Formatting depends on your Locale, see javadocs for Locale class or for any of the classes that wrap around the primitie numeric types. You also have to consider whether you will have users in multiple locales, whether the user locale is the same as the default locale, etc.

oops, spelling s/b “primitive”!

Yes, we’ll have numbers in multiple locals, but we can be arrogant American bastards and make them deal with US nomenclature if we need to.

Thanks for the answers, I’ll talk to the guy today about this a bit and see if he has any realistic reason why this doesn’t work (there’s almost got to be more to this).