- What is String.Format?
- How to use String.Fromat?
- Currency Formation
- DateTime Formation
- 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 the 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
using System; namespace StringFormat_Example { public class Program { public static void Main(string[] args) { decimal coin=17.36m; Console.WriteLine(String.Format("{0:C}",coin)); } } }Output
₹ 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.
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.
Format | Description |
---|---|
{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
using System; namespace StringFormat_Currency { public class Program { public static void Main(string[] args) { decimal dcoin=2398.87m; int icoin=2309; float fcoin=9283.65f; Console.WriteLine(String.Format("Convert Decimal {0:C}",dcoin)); Console.WriteLine(String.Format("Convert Decimal {0:C4}",icoin)); Console.WriteLine(String.Format("Convert Decimal {0:C6}",fcoin)); } } }Output
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.
Format | Description |
---|---|
{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 |
M for months
d for date
h for hour
m for minutes
s for seconds
f for millisecond
z for timezone
Programming Example
using System; namespace StringFormat_DateTime { public class Program { public static void Main(string[] args) { DateTime dt=DateTime.Now; //Start Formation Console.WriteLine(String.Format("Original Value :- {0}",dt)); Console.WriteLine(String.Format("Display Year in 2 Digits :- {0:yy}",dt)); Console.WriteLine(String.Format("Display Year in 4 Digits :- {0:yyyy}",dt)); //Use Capital M for month. Small m displays minutes Console.WriteLine(String.Format("Display Year and Months :- {0:yyyy MM}",dt)); Console.WriteLine(String.Format("Display date, month and Year :- {0:dd MM yyyy}",dt)); Console.WriteLine(String.Format("Display Time :- {0:hh:mm:ss}",dt)); Console.WriteLine(String.Format("My Time Zone is :- {0:zz}",dt)); } } }Output
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
_
DateTimeFormatInfo
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.
Specifier | Property | Pattern (en-us culture) |
---|---|---|
t |
ShortTimePattern | h:mm tt |
d |
ShortDatePattern | M/d/yyyy |
T |
LongTimePattern | h:mm:ss tt |
D |
LongDatePattern | dddd, MMMM dd, yyyy |
f |
(combination of D and t) | dddd, MMMM dd, yyyy h:mm tt |
F |
FullDateTimePattern | dddd, 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,M |
MonthDayPattern | MMMM dd |
y, Y |
YearMonthPattern | MMMM, yyyy |
r, R |
RFC1123Pattern | ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*) |
s |
SortableDateTimePattern | yyyy'-'MM'-'dd'T'HH':'mm':'ss (*) |
u |
UniversalSortableDateTimePattern | yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*) |
Programming Example
using System; namespace StringFormat_DateTimeFormatInfo { public class Program { public static void Main(string[] args) { DateTime dt=DateTime.Now; //Start Formation Console.WriteLine(String.Format("Short Time Pattern [t] : {0:t}",dt)); Console.WriteLine(String.Format("Short Date Pattern [d] : {0:d}",dt)); Console.WriteLine(String.Format("Long Time Pattern [T] : {0:T}",dt)); Console.WriteLine(String.Format("Long Date Pattern [D] : {0:D}",dt)); Console.WriteLine(String.Format("Combination of D and T [f] : {0:f}",dt)); Console.WriteLine(String.Format("Full Date Time Pattern [F] : {0:F}",dt)); Console.WriteLine(String.Format("Combination of d and t [g] : {0:g}",dt)); Console.WriteLine(String.Format("Combination of d and T [G] : {0:G}",dt)); Console.WriteLine(String.Format("Month Day Pattern [m or M] : {0:M}",dt)); Console.WriteLine(String.Format("Year Month Pattern [y or Y] : {0:Y}",dt)); Console.WriteLine(String.Format("RFC1123Pattern [r or R] : {0:R}",dt)); Console.WriteLine(String.Format("SortableDateTimePattern [s] : {0:s}",dt)); Console.WriteLine(String.Format("UniversalSortableDateTimePattern [u] : {0:u}",dt)); } } }Output
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
SortableDateTimePattern [s] : 2017-02-17T09:53:09
UniversalSortableDateTimePattern [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 is 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 TableFormat | Explain | Value | Output |
---|---|---|---|
{0} |
Original Value | 83745.892 | 83745.892 |
{0:0.00} |
Two decimal places after point. | 83745.892 | 83745.89 |
{0:0.##} |
Maximum Two decimal places after point | 83745.892 | 83745.89 |
{0:00.00} |
Two digits before decimal point | 5.892 | 05.892 |
{0:0,0.00} |
Thousands Separator | 83745.892 | 83,745.89 |
{0:0.0} |
Point number conversion with zero | 0.5 | 0.5 |
{0:#.0} |
Point number conversion without zero | 0.5 | .5 |
{0,10:0.0} |
Align Number with Spaces | 120.5 | “ 120.5” |
{0,10:0.0} |
Align Number with Spaces | 120.5 | “120.5 Hello” |
{0:###-##-####} |
Phone Number format | 123456789 | 123-45-6789 |
Programming Example
using System; namespace StringFormat_Double { public class Program { public static void Main(string[] args) { //Start Formation Console.WriteLine("Current Output : {0}", 83745.892); Console.WriteLine(string.Format("Two decimal places : {0:0.00}",83745.892)); Console.WriteLine(string.Format("Three decimal places : {0:0.000}",83745.82)); Console.WriteLine(string.Format("Maximum Two decimal places : {0:0.##}",83745.892)); Console.WriteLine(string.Format("Two digits before decimal point : {0:00.00}",5.892)); Console.WriteLine(string.Format("Thousands Separator : {0:0,0.00}",83745.892)); Console.WriteLine(string.Format("Point number conversion with zero : {0:0.0}",0.5)); Console.WriteLine(string.Format("Point number conversion without zero : {0:#.0}",0.5)); Console.WriteLine(string.Format("Align Number with Spaces : {0,10:0.0}",120.5)); Console.WriteLine(string.Format("Align Number with Spaces : {0,-10:0.0} Hello",120.5)); Console.WriteLine(string.Format("Phone Number format {0:###-##-####}",123456789)); } } }Output
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
_
Summary
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 the user experience more realistic and valuable.