Skip to content
Advertisement

Correct way to format integers with fixed length and space padding

I have integer numbers in the range of 0 up until (including) 100. I want to convert them to strings of a fixed length of 3 with space padding and alignment to the right.

I’ve tried to use the following format string, but it adds another space for three digit numbers, which makes them length 4 instead of 3.

JavaScript

Interestingly, it works as expected when zero-padding is used:

JavaScript

Why is this? How can I fix this?

Do I really need to convert to string first?

JavaScript

Advertisement

Answer

By default, numbers are aligned to the right and padded with spaces when formatted, so you should just specify the width:

JavaScript

Alternatively, you can specify both the fill character and alignment:

JavaScript

A single space before width, however, is treated as the sign option. Quoting the documentation:

The sign option is only valid for number types, and can be one of the following:

'+' indicates that a sign should be used for both positive as well as negative numbers.
'-' indicates that a sign should be used only for negative numbers (this is the default behavior).
' ' indicates that a leading space should be used on positive numbers, and a minus sign on negative numbers.

That’s why "{: 3d}" formats with a leading space in your examples.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement