.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

System Data is Formatted to a String by Using Iformattable Interface

Posted By:Ravi Ranjan Kumar       Posted Date: July 31, 2011    Points: 200    Category: .Net Framework    URL: http://www.dotnetspark.com  

This article enable you to lean how to use Informattable Interface to format System Data to String.


Formatting data may be necessary in any application or code you write. for example, in an example tracking application, you use a class called Employee. you want to represent an object of the Employee class as a string that includes the first and last names of the corresponding employee. depending on the requirement you may need to display the name of the Employee in various formats, sometimes with the first name appearing before the first name. instead of defining two methods to generate each format, you can use the IFormattable instead of defining two methods to generate each format or a specific format.

ToString Method 

the IFormattable  interface defines the ToString method that you can impement to format the value of the current instance by using the specified format. Format can be custom defined according to the need of the implementing class.

The System.Object class provides a default implementation of the ToString method. as a result, each in the .Net Framework has a ToString method attached to it. you can override this ToString method in any of your custom classes to provide your own string representation for an object of the Corresponding class. However, Overriding the ToString method is helpful when the String representation of an object is static or always in the same format.
if you need more flexibility in specifying different formats for the string representation, you need to implement the ToString method that the IFormatable interface provides.

Example - Custom string Formatting for Currency 

the Following code example defines a class to represent a particular price. it implements the IFormatable interface and defines the ToString method to provide a string representation of the corresponding price value in a specific currency format.

class Price:IFormattable
        double price;
        public Price(double value)
            this.price = value;
        public String ToString(String format, IFormatProvider fp)
            if (format == "$")
                return "$" + this.price.ToString("0.00");
            else if (format == null || format.Trim().Equals(""))
                return this.price.ToString();
                return format + " " + this.price.ToString();
how to use Price Class 
take a look below example where I am using Price class to format Currency Values 

Price p = new Price(20.5);
            Console.WriteLine("{0:$}", p);  // $20.50
            Console.WriteLine(p);           //20.0

Output will be 


Reference Link :- 

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend