Tutorials Icon

Tutorials > C# Using .ToString()

C# Using .ToString()

By Stephen Armstrong // November 26, 2019


Learn how to properly display the date, time, and numbers.

When you create a GUI or frontend for your MonoGame project, you may find that some elements (numbers and dates) may need to be turned into strings.

Not only is there a .ToString() method in C# to help you with this, but there are also numerous options you can use to determine the output formatting. This can greatly help you in localizing your game.

Using .ToString() for the Date

The best way to start is with time and date. Add the following code to your game.

// Create a DateTime that represents 10:15pm on November 26, 2019
System.DateTime exampleDateTime = new System.DateTime(2019, 11, 26, 22, 15, 0);

To create a string that contains the date in American formatting (Month/Day/Year) you would use the following code:

string americanDateTime = exampleDateTime.ToString("M/dd/yyyy");

System.Console.WriteLine("American formatting: {0}", americanDateTime);

To create a string that contains the date in British/European formatting (Day/Month/Year) you would use the following:

string britishDateTime = exampleDateTime.ToString("dd/M/yyyy");

System.Console.WriteLine("British/European formatting: {0}", britishDateTime);

And to create a string that contains the date in Asian formatting (Year/Month/Day) you would use the following:

string asianDateTime = exampleDateTime.ToString("yyyy/M/dd");

System.Console.WriteLine("Asian formatting: {0}", asianDateTime);

Add that code to your game, run it and check the Output to see the dates in all three formats.

Using .ToString() for the Time

To display the time value stored in exampleDateTime using a 24-hour clock, use the following code:

string clock24Hours = exampleDateTime.ToString("t");

System.Console.WriteLine("24-hour clock: {0}", clock24Hours);

To display it in a 12-hour clock, use the following code:

string clock12Hours = exampleDateTime.ToString("hh:mm tt");

System.Console.WriteLine("12-hour clock: {0}", clock12Hours);

Add that code to your game, run it and check the Output to see the time in both clock formats.

Using .ToString() for numbers

When writing out long numbers, we generally use a divider after every three digits to denote the beginning of thousands, millions, billions (etc).

.ToString() allows us to not only automatically insert these dividers, but also to localize for Europe where they use a period/full stop instead of a comma.

Add the following decimal to your game.

decimal exampleDecimal = 12345678.90m;

To create a string which contains this decimal as an American/British number (with commas as a large-number divider and a period/full stop as the decimal symbol), use the following code:

string americanNumber = exampleDecimal.ToString("N");
System.Console.WriteLine("American/British number formatting: {0}", americanNumber);

And to create a string which contains this decimal as a European number (periods/full stops for large-number dividers and commas for the decimal symbol), use the following code:

string europeanNumber = exampleDecimal.ToString("N", System.Globalization.CultureInfo.GetCultureInfo("de-DE"));
System.Console.WriteLine("European number formatting: {0}", europeanNumber );

This is just an overview of how .ToString() can be used. Feel free to experiment with it, and maybe there will be further tutorials involving .ToString() sometime in the future.

< Go back

Return to top of page

Article Icon

Welcome to Industrian.net!

On this website you'll find more information about our games, and also some tutorials for you to start making games of your own! You can also follow us on various social platforms!