4

I want to align the number on the string like that:

3 x  89 kg
3 x 133 kg

I use this format:

String.format("%1$3d x %2$3d kg", nb, weight);

But the result is not correct:

enter image description here

2
  • 6
    It seems you are using a proportional font. That means that spaces are narrower than digits. You should use a fixed-width font if you want them to align in this method, e.g. Courier. Commented Sep 12, 2015 at 11:39
  • Unrelated: Since you're just using the values in order, there's no need to specify indexes, so "%3d x %3d kg" would work too. Commented Sep 12, 2015 at 11:42

1 Answer 1

1

This happens due to the font(because the characters don't have the same width) Change the font to a monospaced font and the problem will be solved(I recommend Consolas font for this)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.