It turns out that I was wondering how I can align the result with the string.format to the right 15 characters. I have done it in the following way and it has not worked (it stays in the same place). I attach the code:
using System;
namespace Exercisi_random
{
class Program
{
static void Main(string[] args)
{
int a, b, result;
Console.WriteLine("Insert first number:");
a = int.Parse(Console.ReadLine());
Console.WriteLine("Insert second number:");
b = int.Parse(Console.ReadLine());
result = (a + b) * (a - b);
Console.WriteLine(String.Format("{0,15}\n", "The result of the operation is: {0:f5}", result));
}
}
}
This is the result I expect:
Insert first number: 2
Insert second number: 3
The result of the operation is: -5.00000
Press any key.
This is what it gives me:
Insert first number: 2
Insert second number: 3
The result of the operation is:-5.00000
Press any key.
It is very simple. You only need to specify in the format itself
string
the formats that you are going to apply:As you can see, it is only enough to indicate that the value in the index
0
will take all that space (, 15
) and that it will also write it with a certain format (:f5
). Also note thatWriteLine
it allows you to format the text just like thestring.Format
.