java - 如何在android中格式化longs以始终显示两位数

我有一个倒数计时器,它显示从 60 到 0 的秒数(1 分钟倒数计时器)。当它达到 1 位数字时,例如 9,8,7.. 它显示 9 而不是 09。我尝试使用 String.format("%[B]02d[/B]", x); 我将 x 从 long 转换为 string。没用。

我想要 String.format("%2d", 1)

最佳答案

您可以使用 DecimalFormat 来完成它:

NumberFormat f = new DecimalFormat("00");
long time = 9;
textView.setText(f.format(time));

输出:

09

或者您可以使用 String.format()还有:

String format = "%1$02d"; // two digits
textView.setText(String.format(format, time));

https://stackoverflow.com/questions/20293887/

相关文章:

excel - VBA:将文本转换为数字

java - 清理大型遗留 Java 项目

python - 在 Python 中截断十进制值

c# - string.Format 与 string.Join

c# - 如何在 c# 中生成像 "1st November"这样的日期格式

html - 使 HTML 输出 R 中的粗体文本 Shiny

formatting - Dart 中的货币格式

javascript - 在 JavaScript 源代码中自动插入分号的实用程序?

c# - 格式化跟踪输出

python - !r 在 str() 和 repr() 中做了什么?