如何设置文本格式?

编程入门 行业动态 更新时间:2024-10-23 23:20:12
本文介绍了如何设置文本格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

一个正常的记分牌的文字是这样的。

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 % is 0, the zero-padding flag. This tells the formatter that the result will be zero-padded.
  • After the zero-padding flag is the width, 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

更多推荐

如何设置文本格式?

本文发布于:2023-11-16 16:24:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何设置   文本   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!