C# string interpolation with variable format

后端 未结 6 1576
时光取名叫无心
时光取名叫无心 2021-01-18 06:15

I need to format a variable with string interpolation, and the format string is another variable:

here is my sample code:

static void Main(string[] a         


        
6条回答
  •  感情败类
    2021-01-18 07:11

    I have tested this piece of code and it seems to work:

    static void Main(string[] args)
    {
        int i = 12345;
    
        Console.WriteLine("Test 1: {0:N5}",i);
    
        var formatString = "N5";
    
        Console.WriteLine("Test 2: {0:" + formatString + "}", i);
    
        Console.ReadLine();
    }
    

提交回复
热议问题