Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Text Format?

A normal scoreboard text is like this

enter image description here

so i want to make te text format like this

enter image description here

this is the 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

like image 491
Ricci Avatar asked Aug 24 '26 18:08

Ricci


1 Answers

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.

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

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

Here is an example:

int gamescore = 100;
String result = String.format("%06d", gamescore);
System.out.println(result);

Output: 000100

like image 89
Mitch Talmadge Avatar answered Aug 27 '26 07:08

Mitch Talmadge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!