Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Định dạng đầu ra (Formatted Output) trong C#

Định dạng output cho số (double, decimal) và DateTime dùng String.Format hoặc interpolation $"" với format specifiers.

Định dạng số (Numeric Format Strings)

using System;

class Program
{
    static void Main()
    {
        double d = 987.383;
        double big = 54567.46;
        
        Console.WriteLine("=== Standard Numeric Formats ===");
        Console.WriteLine($"Fixed point (F3): {d:F3}");        // 987.383
        Console.WriteLine($"Fixed point (F3): {987.38:F3}");    // 987.380
        Console.WriteLine($"Fixed point (F3): {987.7899:F3}");  // 987.790
        
        Console.WriteLine($"\nThousands separator (N1): {big:N1}"); // 54,567.5
        Console.WriteLine($"Thousands separator (N0): {big:N0}");   // 54,567
        Console.WriteLine($"Currency (C): {big:C}");                // $54,567.46
        Console.WriteLine($"Percent (P): {0.1234:P}");              // 12.34%
        Console.WriteLine($"Scientific (E): {12345:E}");            // 1.234500E+004
        
        Console.WriteLine("\n=== Custom Numeric Formats ===");
        Console.WriteLine($"00000: {42:D5}");          // 00042
        Console.WriteLine($"#.##: {1.2:0.00}");        // 1.20
        Console.WriteLine($"#,#: {1234567:#,#}");      // 1,234,567
    }
}

Định dạng DateTime

using System;

class Program
{
    static void Main()
    {
        DateTime dt = new DateTime(2018, 2, 8, 12, 7, 7, 123);
        
        Console.WriteLine("=== Standard DateTime Formats ===");
        Console.WriteLine($"Short date (d): {dt:d}");       // 2/8/2018
        Console.WriteLine($"Long date (D): {dt:D}");        // Thursday, February 8, 2018
        Console.WriteLine($"Short time (t): {dt:t}");       // 12:07 PM
        Console.WriteLine($"Long time (T): {dt:T}");        // 12:07:07 PM
        Console.WriteLine($"Full (F): {dt:F}");             // Thursday, February 8, 2018 12:07:07 PM
        Console.WriteLine($"ISO8601 (s): {dt:s}");         // 2018-02-08T12:07:07
        Console.WriteLine($"Round-trip (o): {dt:o}");       // 2018-02-08T12:07:07.1230000
        
        Console.WriteLine("\n=== Custom DateTime Formats ===");
        Console.WriteLine($"yyyy-MM-dd: {dt:yyyy-MM-dd}");   // 2018-02-08
        Console.WriteLine($"HH:mm:ss: {dt:HH:mm:ss}");       // 12:07:07
        Console.WriteLine($"ddd, dd MMM yyyy: {dt:ddd, dd MMM yyyy}"); // Thu, 08 Feb 2018
    }
}

Kết quả đầu ra (mẫu)

=== Standard Numeric Formats ===
Fixed point (F3): 987.383
Fixed point (F3): 987.380
Fixed point (F3): 987.790

Thousands separator (N1): 54,567.5
Thousands separator (N0): 54,567
Currency (C): $54,567.46
Percent (P): 12.34%
Scientific (E): 1.234500E+004

=== Standard DateTime Formats ===
Short date (d): 2/8/2018
Long date (D): Thursday, February 8, 2018
Short time (t): 12:07 PM
Long time (T): 12:07:07 PM
Full (F): Thursday, February 8, 2018 12:07:07 PM
ISO8601 (s): 2018-02-08T12:07:07
Round-trip (o): 2018-02-08T12:07:07.1230000

Lưu ý: Format phụ thuộc CultureInfo.CurrentCulture. Dùng CultureInfo.InvariantCulture cho log/file, CultureInfo.CurrentCulture cho UI người dùng.