如何设置文本格式? [英] How to set Text Format?
问题描述
一个正常的记分牌的文字是这样的。
A normal scoreboard text is like this
所以我想使德文本格式,这样
so i want to make te text format like this
这是code
score = (TextView)findViewById(R.id.ct);
score.setText(String.valueOf(gamescore));
if (gamescore > highscore){
high.setText(String.valueOf(gamescore));
}
任何人都可以解释一下吗?感谢的
Anyone can explain? Thank's
推荐答案
您应该使用的String.format()用的方法语法相对=nofollow>格式化语法。 的String.format()
可用于左垫0的字符串,就像你想要的。
You should use the String.format() method along with the formatter syntax. String.format()
can be used to left-pad a string with zeroes, just like you want.
这code会给你你想要的结果: high.setText(的String.format(%06D,gamescore));
This code will give you the results that you want: high.setText(String.format("%06d", gamescore));
让我们来看看具体是怎么回事。我们正在使用的语法如下:%[国旗] [宽度]转换
Let's look at what's going on in detail. We are using this syntax: %[flags][width]conversion
- 总是开始于一个
%
。 格式字符串语法 - 继
%
是0
,零填充标志
。这告诉格式化,其结果将是零填充。 - 补零
标志
后是宽度
,或者我们想要的最小位数;在这种情况下,6
。 - 最后,指定转换类型。期望的结果被格式化为十进制整数,所以我们使用
D
。
- Always begin the format string syntax with a
%
. - Following
%
is0
, the zero-paddingflag
. This tells the formatter that the result will be zero-padded. - After the zero-padding
flag
is thewidth
, or the minimum number of digits we want; in this case,6
. - Lastly, specify the conversion type. The desired result is formatted as a decimal integer, so we use
d
.
下面是一个例子:
int gamescore = 100;
String result = String.format("%06d", gamescore);
System.out.println(result);
输出: 000100
这篇关于如何设置文本格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!