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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1942118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:38:25  来源:igfitidea点击:

How do I format a long integer as a string without separator in Java?

javamessageformat

提问by Daniel Fortunov

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”。

采纳答案by Rob H

回答by Daniel Fortunov

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

回答by Frank Grimm

As an alternative String.formatand java.util.Formattermight work for you as well...

作为替代方案String.formatjava.util.Formatter也可能对您有用......

回答by mportiz08

You could try:

你可以试试:

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

回答by Peter Lawrey

The shortest way is

最短的路是

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

回答by Christopher Schultz

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.ChoiceFormatis for.

在尝试使用国际化等方式实现“真实世界”模式时,我对此有点挣扎。具体来说,我们需要使用“选择”格式,其中输出取决于显示的值,这就是java.text.ChoiceFormat目的。

Here is an example for how to get this done:

以下是如何完成此操作的示例:

    MessageFormat fmt = new MessageFormat("{0,choice,0#zero!|1#one!|1<{0,number,'#'}|10000<big: {0}}");

    int[] nums = new int[] {
            0,
            1,
            100,
            1000,
            10000,
            100000,
            1000000,
            10000000
    };

    Object[] a = new Object[1];
    for(int num : nums) {
        a[0] = num;
        System.out.println(fmt.format(a));
    }

This generates the following output; I hope it's helpful for others who are trying to accomplish the same types of things:

这将生成以下输出;我希望它对其他试图完成相同类型事情的人有所帮助:

zero!
one!
100
1000
10000
big: 100,000
big: 1,000,000
big: 10,000,000

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.

如您所见,“选择”格式允许我们根据要格式化的传入值来选择要使用的格式类型。小数字可以用文字代替(不显示原值)。显示中等大小的数字,没有分组分隔符(没有逗号)。最大的数字也包括逗号。显然,这是一个完全人为的例子来展示java.text.MessageFormat.

A note about the quoted #in the format text: since both ChoiceFormatand MessageFormatare being used, there is a collision between metacharacters between the two. ChoiceFormatuses #as a metacharacter that essentially means "equals" so that the formatting engine knows that e.g. in the case of 1#one!we are comparing {0}with 1, and if they are equal, it uses that particular "choice".

关于#格式文本中引用的注意事项:由于ChoiceFormatMessageFormat都被使用,因此两者之间的元字符之间存在冲突。ChoiceFormat用途#元字符,基本上的意思是“等于”,这样的格式化引擎知道,例如,在的情况下1#one!,我们比较{0}1,如果他们是平等的,它使用的是特定的“选择”。

But #has another meaning to MessageFormat, and that's as a metacharacter which has meaning for DecimalFormat: it's a metacharacter which means "put a number here".

但是#对于 有另一个含义MessageFormat,那就是作为一个具有含义DecimalFormat的元字符:它是一个元字符,意思是“在此处放置一个数字”。

Because it's wrapped up in a ChoiceFormatstring, the #needs to be quoted. When ChoiceFormatis done parsing the string, those quotes are removed when passing the subformats to MessageFormat(and then on to DecimalFormat).

因为它被包裹在一个ChoiceFormat字符串中,所以#需要用引号引起来。当ChoiceFormat完成解析字符串,路过时,子格式到这些报价被删除MessageFormat(然后到DecimalFormat)。

So when you are using {0,choice,...}, you have to quote those #characters, and possibly others.

因此,当您使用 时{0,choice,...},您必须引用这些#字符,可能还有其他字符。