Category Archives: C# Articles

String.Format C# – Everything You Need to Know

In this tutorial you will learn

  1. What is String.Format?
  2. How to use String.Fromat?
  3. Currency Formation
  4. DateTime Formation
  5. Decimal, Double, Float and Int Formation

String.Format converts the value according the Format specified by you. In this tutorial you will learn all techniques to convert every type of value in desired format.

Demo Program

Here, I am writing a demo program to make you clear how String.Format works. Here I have some decimal value and I will convert them into currency. The currency icon will be displayed according to your locale settings.

Programming Example


₹ 17.36

It is just a demo program and there is wide range of String.Format formation available. In this article I have tried to combine all Formation that will help you to learn entire aspect in one article.

In this Chapter you will learn


String.Format for Currency Conversion

Most of the time you need to display integer, double or number value into currency. You don’t need to write dozens line of code for it. Just let String.Format in action and customize value for it.

{0:C}It displays the actual value with currency
{0:C4}It displays actual value along with 4 digit after point precision.
{0:C6}It displays actual value along with 6 digit after point precision.

Programming Example


Convert Decimal ₹ 2,398.87
Convert Decimal ₹ 2,309.0000
Convert Decimal ₹ 9,283.650000


String.Format for DateTime Conversion

This is the area where String.Format is widely used. A simple DateTime program doesn’t give formatted output. String.Format in C# helps you to display DateTime according to your need. You can apply following formation on DateTime.

{0:yy}Displays Year in 2 Digits
{0:yyyy}Displays Year in 4 Digits
{0:MM}Displays Month. Use Capital ‘M’ for month because small m for minutes.
{0:dd}Displays Day Date
{0:dd-MM-yyyy}Displays date in dd-MM-YYYY format.
{0:hh:mm:ss}Displays Time in hh:mm:ss format.
{0:zz}Displays Current TimeZone
Always remember while formatting date
y for Years
M for months
d for date
h for hour
m for minutes
s for seconds
f for millisecond
z for timezone

Programming Example


Original Value :- 17/02/17 9:56:00 AM
Display Year in 2 Digits :- 17
Display Year in 4 Digits :- 2017
Display Year and Months :- 2017 02
Display date, month and Year :- 17 02 2017
Display Time :- 09:56:00
My Time Zone is :- +05


There are some defined standards in DateTimeFormatInfo that will help you to customize Date Time in the easiest way. Here is the list of Defined Standard for en-us culture.

SpecifierPropertyPattern (en-us culture)
tShortTimePatternh:mm tt
TLongTimePatternh:mm:ss tt
DLongDatePatterndddd, MMMM dd, yyyy
f(combination of D and t)dddd, MMMM dd, yyyy h:mm tt
FFullDateTimePatterndddd, MMMM dd, yyyy h:mm:ss tt
g(combination of d and t)M/d/yyyy h:mm tt
G(combination of d and T)M/d/yyyy h:mm:ss tt
m,MMonthDayPatternMMMM dd
y, YYearMonthPatternMMMM, yyyy
r, RRFC1123Patternddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ (*)
sSortableDateTi­mePatternyyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss (*)
uUniversalSorta­bleDateTimePat­ternyyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’ (*)

Programming Example


Short Time Pattern [t] : 9:53 AM
Short Date Pattern [d] : 17/02/17
Long Time Pattern [T] : 9:53:09 AM
Long Date Pattern [D] : Friday 17 February 2017
Combination of D and T [f] : Friday 17 February 2017 9:53 AM
Full Date Time Pattern [F] : Friday 17 February 2017 9:53:09 AM
Combination of d and t [g] : 17/02/17 9:53 AM
Combination of d and T [G] : 17/02/17 9:53:09 AM
Month Day Pattern [m or M] : 17 February
Year Month Pattern [y or Y] : February 2017
RFC1123Pattern [r or R] : Fri, 17 Feb 2017 09:53:09 GMT
SortableDateTi­mePattern [s] : 2017-02-17T09:53:09
UniversalSorta­bleDateTimePat­tern [u] : 2017-02-17 09:53:09Z


String.Format for Double, Decimal, Float or Int

Several times you need to get customize output for Double, Decimal or Float value. Sometimes output in this datatype are too long after point precision and you just need to cut rest of the value after point precision. String.Format helps you lot in customizing Double, Decimal or Float value.

Formatting Table

{0}Original Value83745.89283745.892
{0:0.00}Two decimal places after point.83745.89283745.89
{0:0.##}Maximum Two decimal places after point83745.89283745.89
{0:00.00}Two digits before decimal point5.89205.892
{0:0,0.00}Thousands Separator83745.89283,745.89
{0:0.0}Point number conversion with zero0.50.5
{0:#.0}Point number conversion without zero0.5.5
{0,10:0.0}Align Number with Spaces120.5“ 120.5”
{0,10:0.0}Align Number with Spaces120.5“120.5 Hello”
{0:###-##-####}Phone Number format123456789123-45-6789

Programming Example


Current Output : 83745.892
Two decimal places : 83745.89
Three decimal places : 83745.820
Maximum Two decimal places : 83745.89
Two digits before decimal point : 05.89
Thousands Separator : 83,745.89
Point number conversion with zero : 0.5
Point number conversion without zero : .5
Align Number with Spaces : 120.5
Align Number with Spaces : 120.5 Hello
Phone Number format 123-45-6789


In this tutorial I have tried to explain String.Format widely with suitable and realistic programming example. It is very important for every programmer to know how to use String.Format in their program. It makes user experience more realistic and valuable.

C# string vs String? What are the Differences?

As a developer are you confused to choose between string and System.String?
In this article I am going to show you all the differences between string and System.String in C# with proper programming example. Continue reading C# string vs String? What are the Differences?

Answer – C# Event Handling Exercises

Qu: Write a program for Online Attendance. The conditions are as follow:

  1. User provides their name as Input and then application show message to “Welcome to their Name”.
  2. Jack, Steven and Mathew are banned for the organization. So, when any user enters Jack, Steven and Mathew as user name, the application raised an event and fire alarm as well as sends email to administration.

Continue reading Answer – C# Event Handling Exercises

C# – How to Use Const in Inheritance

In this tutorial, you will learn to use const in Inheritance.

Programming Example

This example contains a class Laptop which inherits be base class LaptopBase. LaptopBase has several const values that are used in main method.

This program is showing different types of laptop with details. All the details are constant and cannot be changed.

Continue reading C# – How to Use Const in Inheritance