0Pricing
C# Academy · Lesson

Numeric and Date Format Specifiers

Control how numbers and dates render.

Numeric and Date Format Specifiers is a free C# Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Standard Numeric Formats

Format specifiers are short codes that shape how numbers display. Common ones are N, C, P, F, and D.

using System;

class Program {
    static void Main() {
        double n = 1234.5;
        Console.WriteLine($"{n:N2}");
    }
}

Number Format: N

N2 formats a number with thousands separators and two decimal places.

using System;

class Program {
    static void Main() {
        Console.WriteLine($"{1234567.891:N2}");
    }
}

Currency Format: C

C formats a number as currency using the current culture symbol and grouping.

using System;
using System.Globalization;

class Program {
    static void Main() {
        Console.WriteLine(19.99m.ToString("C", CultureInfo.GetCultureInfo("en-US")));
    }
}

Percent Format: P

P multiplies by 100 and appends a percent sign. 0.25 becomes 25 percent.

using System;
using System.Globalization;

class Program {
    static void Main() {
        Console.WriteLine((0.25).ToString("P1", CultureInfo.InvariantCulture));
    }
}

Fixed-Point: F

F3 shows a fixed number of decimal places without thousands separators.

using System;

class Program {
    static void Main() {
        Console.WriteLine($"{3.14159:F3}");
    }
}

Padded Integers: D

D5 pads an integer with leading zeros to at least five digits.

using System;

class Program {
    static void Main() {
        Console.WriteLine($"{42:D5}");
    }
}

Hexadecimal: X

X formats an integer as hexadecimal; X4 pads to four hex digits.

using System;

class Program {
    static void Main() {
        Console.WriteLine($"{255:X}");
        Console.WriteLine($"{255:X4}");
    }
}

Custom Numeric Patterns

Use a custom pattern like 0.00 or #,##0 for precise control over digits and grouping.

using System;
using System.Globalization;

class Program {
    static void Main() {
        Console.WriteLine((1234.5).ToString("#,##0.00", CultureInfo.InvariantCulture));
    }
}

Date Format Specifiers

Dates also have specifiers: d short date, D long date, t short time, and custom patterns like yyyy-MM-dd.

using System;
using System.Globalization;

class Program {
    static void Main() {
        var date = new DateTime(2026, 5, 30);
        Console.WriteLine(date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
    }
}

Custom Date Patterns

Compose patterns from tokens: dd day, MM month, yyyy year, HH:mm time.

using System;
using System.Globalization;

class Program {
    static void Main() {
        var dt = new DateTime(2026, 5, 30, 14, 5, 0);
        Console.WriteLine(dt.ToString("dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture));
    }
}

Specifiers in Interpolation

All these specifiers work after a colon inside interpolated strings too.

using System;

class Program {
    static void Main() {
        double total = 1999.5;
        Console.WriteLine($"Total: {total:N2}");
    }
}

Quick Check

Test your knowledge of format specifiers.

Recap

Standard numeric specifiers shape output: N grouped number, C currency, P percent, F fixed decimals, D zero-padded integer, and X hex. Custom patterns like #,##0.00 offer fine control.

Date specifiers (d, D, yyyy-MM-dd) work the same way, and all of them apply after a colon in interpolation or string.Format.

Frequently asked questions

Is the “Numeric and Date Format Specifiers” lesson free?

Yes — the full text of “Numeric and Date Format Specifiers” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Numeric and Date Format Specifiers”?

Control how numbers and dates render. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Numeric and Date Format Specifiers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. String Interpolation Basics
  2. Composite Formatting with string.Format
  3. Numeric and Date Format Specifiers
  4. Raw String Literals
← Back to C# Academy