Casting , Convert and Format data

Standard

Various reason one data type convert into another type explicitly (using casting method to convert the data type ) or implicitly (assign one data type value directly into another data type variable without any error) in software programing. Especially for the calculation.

Once done the calculation data should present in the readable format. .Net allow us to display the data in various format. (String.Format(string Format), DateTime.Tostring(string Format) .

using System;

using System.Globalization;

namespace FormatConvertData

{

class Program

{

static void Main(string[] args)

{

string arg = Console.ReadLine();

Console.WriteLine(String.Format("Display the decimal data in default currency format from 6 cursor position left {0,6:C} \r\n", Convert.ToDecimal(arg)));

//Note: ‘0’ – parameter ‘,6’ – position left ‘:C’ – default currency

Console.WriteLine(String.Format("Display the decimal data in default currency format from 2 cursor position right is {0,-2:C} \r\n", Convert.ToDecimal(arg)));

//Note: ‘0’ – parameter ‘,-2’ – position left ‘:C’ – default currency

Console.WriteLine(String.Format("Display the decimal data in Scientific notation is {0:E} \r\n", Decimal.Parse(arg)));

//Note: ‘0’ – parameter ‘:E’ – scientific form

Console.WriteLine(String.Format("Display the only integer data in general integer format (Converting the string to integer using multicasting) is {0:D} \r\n", (int)(Convert.ToDecimal(arg))));

//Note: ‘0’ – parameter ‘:D’ – General integer

Console.WriteLine(String.Format("Display the decimal data into General number is {0:G} \r\n", Convert.ToDecimal(arg)));

//Note: ‘0’ – parameter ‘:D’ – General number

Console.WriteLine(String.Format("Display the decimal data into is percentage {0:P} \r\n", Convert.ToDecimal(arg)));

//Note: ‘0’ – parameter ‘:P’ – percentage

Console.WriteLine(String.Format("Display the integer data in Hexadecimal format is {0:X} \r\n", (Int32)(Convert.ToDecimal(arg))));

//Note: ‘0’ – parameter ‘:X’ – Hexadecimal

Console.WriteLine(String.Format("Display the decimal data into common locale format is {0:N} \r\n", Convert.ToDecimal(arg)));

//Note: ‘0’ – parameter ‘:N’ – common locale format

Console.WriteLine(String.Format("Display the value in current cultrue : {0}", Convert.ToDecimal(arg).ToString("C", CultureInfo.CurrentCulture)));

Console.WriteLine(String.Format("Display the value using ‘ja-JA’ – japanese culture : {0}", Convert.ToDecimal(arg).ToString("C3", CultureInfo.CreateSpecificCulture("ja-JA"))));

Console.WriteLine("Enter date value MM/dd/YYYY format : ");

arg = Console.ReadLine();

DateTime dt;

Console.WriteLine("Format the date using Convert.ToDateTime: {0}", (dt = Convert.ToDateTime(arg)).ToLongDateString());

Console.WriteLine("Format the date using Convert.ToDateTime: {0}", Convert.ToDateTime(arg).ToString("dd-MM-yyyy HHmm"));

Console.WriteLine("Format the date using string.Format : {0}", string.Format("{0:ddddd, MMMMM, yyyy hh:mm:ss:fffff tt zzzz}", dt));

Console.ReadLine();

 

// System.Globalization.CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
//            System.Globalization.TextInfo textinfo = cultureInfo.TextInfo ;
//            return textinfo.ToTitleCase("rose");

}

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s