Console.WriteLine("{0:f4}",c); 

As in this line, instead of f4 , in place of 4, substitute a variable into which the required number of characters will fall

  • the first parameter is a regular string, respectively, and you can collect it as you like, using whatever variables you like - Grundy

2 answers 2

The first parameter is a regular string, respectively, and you can collect it as you like, using whatever variables you want, starting with the usual concatenation

 Console.WriteLine("{0:f"+i+"}",c); 

Ending with interpolation of strings , which allows you to get away from a direct call to string.Format

 Console.WriteLine($"{{0:f{i}}}",c); 
     Console.WriteLine(string.Format("{{0:f{0}}}", i),c);