How to display a number so that it takes a certain number of characters?
Example:
There are numbers 321 and 34, it is necessary that they occupy 5 characters.
That is, "__321" and "___34", respectively (earth = space)

  • And why do you need such a conclusion and what is the essence of the program? - Artik Slayer

2 answers 2

You can, for example, like this:

int i = 123; Console.WriteLine("{0,5}", i); 

Where in the format string: 5 is the total length of the displayed number.

    It is possible through the specifier width format.

    Here is the code:

     int n = 5; string s = string.Format("|{0}| |{1,10}|", n, n); Console.WriteLine(s); 

    gives out:

     |5| | 5| 

    If you do not use string.Format or other similar functions, you can use PadLeft :

     s = n.ToString().PadLeft(10);