Blog options

Formatting Strings and Numbers in C#

Formatting Strings and Numbers in C#

One of the first things we do when learning C# is log messages to a console. We do so by using a console writeline command like this:

Console.WriteLine("Hello World");
//prints: Hello World

Of course logging static strings to the console can be a great debugging tool, but it's usefulness increases substantially when we interpolate dynamic values into our strings, which can be done in the following manor:

string myName = "Dan";
Console.WriteLine("My name is {0}", myName);
// prints: My name is Dan

If you're not familiar with what's going on here, I'll briefly explain. The first parameter to our Console.WriteLine is a string literal, which contains an optional placeholder denoted by a set of curly braces that wrap a number. The second argument represents the value we wish to pass into the placeholder.

In the above example, we have just one argument to interpolate, so we use the number zero to represent where our first argument shall be placed. It's important to note that our placeholders must always begin with the number zero and increment sequentially. Let's take a look at how we might pass multiple arguments into our string.

string firstName = "Dan";
string lastName = "Beaulieu";
Console.WriteLine("last name: {1}, first name: {0}", firstName, lastName);
// prints: last name: Beaulieu, first name: Dan 
Number Formatters

Now that we've hashed out the basics, we can explore number formatters without further hesitation.

String Format
Character Meaning
C or c
We use {0:c} to format currency. Which uses the local cultural prefix by default. So, $ in America.
D or d
We use {0:d} to format decimal numbers. This formatter also optionally allows us to set a minimum number of digits for padding, which we'll demonstrate in our example below.
E or e
We use {0:e} for exponential notation. We can control the exponential constant's casing by using a capital or lower-case E.
F or f
We use {0:f} for fixed-point formatting. As with our decimal formatter, this flag also takes an optional number to designate the minimum number of digits to pad.
N or n
We use {0:n} for standard numerical formatting with commas.
X or x
We use {0:x} for hexadecimal formatting, if we want our formatting to include capital letters we simply use a capital X in as our flag like so {0:X}


Example Usage

static void Main(string[] args)
    string firstName = "Dan";
    string lastName = "Beaulieu";
    string formatted = string.Format("last, first: {1}, {0}", firstName, lastName);
    Console.WriteLine(formatted); // prints: last, first: Beaulieu, Dan
    Console.WriteLine("c format: {0:c}", 123456); // c format: $123,456.00
    Console.WriteLine("n format: {0:n}", 123456); // n format: 123,456.00
    Console.WriteLine("g format: {0:g}", 123456); // g format: 123456
    Console.WriteLine("E format: {0:E}", 123456); // E format: 1.234560E+005
    Console.WriteLine("e format: {0:e}", 123456); // e format: 1.234560e+005
    Console.WriteLine("X format: {0:X}", 123456); // X format: 1E240
    Console.WriteLine("x format: {0:x}", 123456); // x format: 1e240
    Console.WriteLine("d format: {0:d9}", 123456); // d8 format: 00123456
    Console.WriteLine("f format: {0:f4}", 123456); // f3 format: 123456.0000

Hope someone out there found this useful!