如何将 Java 的 AtomicInteger 值转换为三位数

huangapple go评论72阅读模式
英文:

How to get java AtomicInteger value into three digits

问题

我想要一个以 001002003 等方式开始的计数器。如何在使用 Java 的 AtomicInteger 进行实现?我只能得到 1、2、3...

AtomicInteger counter = new AtomicInteger(0);
System.out.println(String.format("%03d", counter.incrementAndGet()));
英文:

I want to have a counter starting like 001 and 002, 003 likewise. How to get it using java AtomicInteger? I can only get 1,2,3...

AtomicInteger counter = new AtomicInteger(0);
System.out.println(counter.incrementAndGet());

答案1

得分: 2

String.format("%03d", 5) 会返回字符串 "005"。你可以将 System.out.print(String.format(pattern, values)) 缩写为 System.out.printf(pattern, values)(如果你想要换行,可以在模式字符串末尾加上 \n)。

这种格式化特性与模式一起使用。% 开始一个模式。d 表示:一个整数(f 用于浮点数),3 表示:至少显示 3 个字符,如果需要可以显示更多,而 0 表示:如果数字不足 3 个字符,则用零进行填充。因此,%03d 是这样的模式:显示该数字,如果小于 100,则用零填充,以确保为 3 位数。

想要了解更多信息,请查阅 String.format 的 javadoc。

英文:

String.format("%03d", 5) would return the string "005". You can shortcut System.out.print(String.format(pattern, values)) to just System.out.printf(pattern, values) (if you want the newline, toss an \n in your pattern string at the end).

This formatting feature works with patterns. % starts a pattern. d means: An integral number (f is for floating point numbers), 3 means: Render at least 3 characters, more if needed, and 0 means: pad it up with zeroes if you need padding because the number is less than 3 characters. Thus, %03d is the pattern for: Render the number, if less than 100 pad with zeroes so it's 3 digits.

Read up on the javadoc of String.format for more info.

答案2

得分: 1

因为Java仅在内部存储数字本身,所以您需要自己处理此操作。
但是,通过java.lang.String.format(String,Object...)System.out.printf可以很容易地实现,如下所示:

AtomicInteger counter = new AtomicInteger(0);
System.out.printf("%03d\n", counter.incrementAndGet());
counter.set(50);
System.out.println(String.format("%03d", counter.incrementAndGet()));
counter.set(100);
System.out.println(String.format("%03d", counter.incrementAndGet()));

输出:

001
051
101
英文:

As java does only store the number itself internally, you have to do that yourself.
But this is quite easily done with java.lang.String.format(String,Object...) or System.out.printf like this:

AtomicInteger counter = new AtomicInteger(0);
System.out.printf("%03d\n", counter.incrementAndGet());
counter.set(50);
System.out.println(String.format("%03d", counter.incrementAndGet()));
counter.set(100);
System.out.println(String.format("%03d", counter.incrementAndGet()));

Output:

001
051
101

答案3

得分: 0

因为这是AtomicInteger的toString()方法返回的结果。如果你想要零值,可以自己创建一个方法来实现。记住,它们现在是字符串。

英文:

It behaves that way because that's the the AtomicInteger's method toString() returns. If you want zeroes, just make your own method for it. Remember they are now strings.

huangapple
  • 本文由 发表于 2020年10月15日 21:51:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64373087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定