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

发布于 2024-08-15 09:02:44 字数 200 浏览 2 评论 0原文

简单的问题,但我敢打赌,在这里提问可能比尝试理解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花桑 2024-08-22 09:02:44
MessageFormat.format("{0,number,#}", foo);
MessageFormat.format("{0,number,#}", foo);
悍妇囚夫 2024-08-22 09:02:44

当我尝试通过国际化等来实现“现实世界”模式时,我在这个问题上遇到了一些困难。具体来说,我们需要使用“选择”格式,其中输出取决于显示的值,这就是 java.text.ChoiceFormat 用于。

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

    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));
    }

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

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

如您所见,“选择”格式允许我们根据传入要格式化的值来选择要使用的格式类型。小数字可以用文本替换(不显示原始值)。显示中等大小的数字,不带分组分隔符(无逗号)。最大的数字再次包含逗号。显然,这是一个完全人为设计的示例,旨在演示 java.text.MessageFormat 的灵活性。

关于格式文本中引用的 # 的注意事项:由于同时使用 ChoiceFormatMessageFormat,因此两者之间的元字符之间存在冲突。 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:

    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.

A note about the quoted # in the format text: since both ChoiceFormat and MessageFormat 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 of 1#one! we are comparing {0} with 1, and if they are equal, it uses that particular "choice".

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".

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

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

我的影子我的梦 2024-08-22 09:02:44

最短路线是

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

The shortest way is

long foo = 12345;
String s = ""+foo;
风为裳 2024-08-22 09:02:44

作为替代方案 String.formatjava.util.Formatter 可能也适合您......

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

若水微香 2024-08-22 09:02:44

你可以尝试:

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

You could try:

String s = new Long(foo).toString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文