String format: {0,3:D}, {1,8:D} : String Format « String « C# / CSharp Tutorial






using System;  
  
class MainClass {  
  public static void Main() {  
    int sum = 0, prod = 1;
    for(int i=1; i <= 10; i++) { 
      sum += i; 
      prod *= i; 
      string str = String.Format("Sum:{0,3:D}  Product:{1,8:D}", 
                          sum, prod); 
      Console.WriteLine(str); 
    } 
  } 
}
Sum:  1  Product:       1
Sum:  3  Product:       2
Sum:  6  Product:       6
Sum: 10  Product:      24
Sum: 15  Product:     120
Sum: 21  Product:     720
Sum: 28  Product:    5040
Sum: 36  Product:   40320
Sum: 45  Product:  362880
Sum: 55  Product: 3628800








5.14.String Format
5.14.1..NET String Format Characters
5.14.2.Use String.Format() to format a value.
5.14.3.String format: {0,3:D}, {1,8:D}
5.14.4.Use ToString to format double: C
5.14.5.String format: {0:C}
5.14.6.Formatting Using the R Format Specifier
5.14.7.String and WriteLine Format Specifiers