如何在 Java 中将长整数格式化为不带分隔符的字符串?
简单的问题,但我敢打赌,在这里提问可能比尝试理解 MessageFormat
的文档更直接:
long foo = 12345;
String s = MessageFormat.format("{0}", foo);
观察到的值为“12,345”。
所需值为“12345”。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需使用
Long.toString (长foo)
Just use
Long.toString(long foo)
当我尝试通过国际化等来实现“现实世界”模式时,我在这个问题上遇到了一些困难。具体来说,我们需要使用“选择”格式,其中输出取决于显示的值,这就是
java.text.ChoiceFormat
用于。以下是如何完成此操作的示例:
这会生成以下输出;我希望这对尝试完成相同类型事情的其他人有所帮助:
如您所见,“选择”格式允许我们根据传入要格式化的值来选择要使用的格式类型。小数字可以用文本替换(不显示原始值)。显示中等大小的数字,不带分组分隔符(无逗号)。最大的数字再次包含逗号。显然,这是一个完全人为设计的示例,旨在演示 java.text.MessageFormat 的灵活性。
关于格式文本中引用的
#
的注意事项:由于同时使用ChoiceFormat
和MessageFormat
,因此两者之间的元字符之间存在冲突。ChoiceFormat
使用#
作为元字符,本质上意味着“等于”,以便格式化引擎知道,例如在1#one!
的情况下,我们正在将{0}
与1
进行比较,如果它们相等,则使用该特定的“选择”。但是
#
对MessageFormat
还有另一个含义,那就是作为一个元字符,对DecimalFormat
也有意义:它是一个元字符,意思是“在此处输入一个数字” ”。由于它包含在
ChoiceFormat
字符串中,因此需要用引号引起来#
。当ChoiceFormat
解析完字符串后,将子格式传递给MessageFormat
(然后传递给DecimalFormat
)时,这些引号将被删除。因此,当您使用
{0,choice,...}
时,您必须引用这些#
字符,可能还有其他字符。I struggled with this a little bit when trying to do "real world" patterns with internationalization, etc. Specifically, we have a need to use a "choice" format where the output depends upon the values being displayed, and that's what
java.text.ChoiceFormat
is for.Here is an example for how to get this done:
This generates the following output; I hope it's helpful for others who are trying to accomplish the same types of things:
As you can see, the "choice" format allows us to choose the type of format to use depending upon the value being passed-in to be formatted. Small numbers can be replaced with text (no display of the original value). Medium-sized numbers are shown with no grouping separators (no commas). The largest numbers do include the commas, again. Obviously, this is an entirely contrived example to demonstrate the flexibility of
java.text.MessageFormat
.A note about the quoted
#
in the format text: since bothChoiceFormat
andMessageFormat
are being used, there is a collision between metacharacters between the two.ChoiceFormat
uses#
as a metacharacter that essentially means "equals" so that the formatting engine knows that e.g. in the case of1#one!
we are comparing{0}
with1
, and if they are equal, it uses that particular "choice".But
#
has another meaning toMessageFormat
, and that's as a metacharacter which has meaning forDecimalFormat
: it's a metacharacter which means "put a number here".Because it's wrapped up in a
ChoiceFormat
string, the#
needs to be quoted. WhenChoiceFormat
is done parsing the string, those quotes are removed when passing the subformats toMessageFormat
(and then on toDecimalFormat
).So when you are using
{0,choice,...}
, you have to quote those#
characters, and possibly others.最短路线是
The shortest way is
作为替代方案
String.format
和java.util.Formatter
可能也适合您......As an alternative
String.format
andjava.util.Formatter
might work for you as well...你可以尝试:
You could try: