如何在Java中将长整数格式化为不带分隔符的字符串?

时间:2021-04-27 07:27:36

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

简单的问题,但我敢打赌,在这里询问可能比试图理解MessageFormat的文档更直截了当:

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

Observed value is "12,345".

观察值为“12,345”。

Desired value is "12345".

期望值是“12345”。

5 个解决方案

#1


48  

Just use Long.toString(long foo)

只需使用Long.toString(long foo)

#2


256  

MessageFormat.format("{0,number,#}", foo);

#3


0  

The shortest way is

最短的路是

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

#4


-1  

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

作为替代方案,String.format和java.util.Formatter可能也适合你...

#5


-5  

You could try:

你可以尝试:

String s = new Long(foo).toString();

#1


48  

Just use Long.toString(long foo)

只需使用Long.toString(long foo)

#2


256  

MessageFormat.format("{0,number,#}", foo);

#3


0  

The shortest way is

最短的路是

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

#4


-1  

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

作为替代方案,String.format和java.util.Formatter可能也适合你...

#5


-5  

You could try:

你可以尝试:

String s = new Long(foo).toString();