views:

259

answers:

6

Simple question, but I'll bet that asking on here will probably be more straight forward than trying to understand the documentation for MessageFormat:

long foo = 12345;
String s = MessageFormat.format("{0}", foo);

Observed value is "12,345".

Desired value is "12345".

+6  A: 

Just use Long.toString(foo)

Rob H
Or `String.valueOf(long)`
rsp
String.valueOf() calls Long.toString()
Peter Lawrey
Maybe this is trifling but in this case you're relying on an undocumented behavior of Long.toString(foo). For that reason, I prefer Daniel Fortunov's answer.
John K
It's not undocumented. See http://download.oracle.com/javase/6/docs/api/java/lang/Long.html#toString%28long%29.
Rob H
Rob H: Oh, you're right, though I'd point at the docs for Long#toString(long, int)
John K
+1  A: 

Take a look at (http://coffeaelectronica.com/blog/2009/11/messageformat-goodies/) line 23 of the code example shows the custom number format. For your case you can just use #### and it should avoid adding commas.

Hope this helps.

cjstehno
+7  A: 
MessageFormat.format("{0,number,#}", foo);
Daniel Fortunov
A: 

As an alternative String.format and java.util.Formatter might work for you as well...

Frank Grimm
A: 

You could try:

String s = new Long(foo).toString();
mportiz08
A: 

The shortest way is

long foo = 12345;
String s = ""+foo;
Peter Lawrey