27 Haz

C# String Formatting For A Number Using Scientific Notation

This example shows how to format a number using scientific notation. To do this, you can use string.Format method.

Samples:

            double number1 = 12345.67890123;
            Console.WriteLine("Default Format: " + number1);
            Console.WriteLine("Scientific Notation: " + string.Format("{0:#.##E+0}", number1));
            Console.WriteLine();

            int number2 = 123456789;
            Console.WriteLine("Default Format: " + number2);
            Console.WriteLine("Scientific Notation: " + string.Format("{0:#.##E+0}", number2));
            Console.WriteLine();

            double number3 = 12345.67890123;
            Console.WriteLine("Default Format: " + number3);
            Console.WriteLine("Scientific Notation: " + string.Format("{0:#.##E+00}", number3));
            Console.WriteLine();

Output is like below:

Default Format: 12345.67890123
Scientific Notation: 1.23E+4

Default Format: 123456789
Scientific Notation: 1.23E+8

Default Format: 12345.67890123
Scientific Notation: 1.23E+04
26 Haz

C# Indent String With Spaces

This example shows how to indent strings using method for padding in C#. “String.PadLeft” static method is used to repeat spaces.

Usage:

            Console.WriteLine(Indent(0) + "Versions");
            Console.WriteLine(Indent(2) + "Version 1");
            Console.WriteLine(Indent(4) + "Version 1.1");
            Console.WriteLine(Indent(4) + "Version 1.2");
            Console.WriteLine(Indent(6) + "Version 1.2.1");
            Console.WriteLine(Indent(2) + "Version 2");
            Console.WriteLine(Indent(4) + "Version 2.1");
            Console.WriteLine(Indent(4) + "Version 2.2");
        public static string Indent(int count)
        {
            return "".PadLeft(count);
        }

Output is like below.

Versions
  Version 1
    Version 1.1
    Version 1.2
      Version 1.2.1
  Version 2
    Version 2.1
    Version 2.2
16 Haz

C# Convert A Numeric Value To Hexadecimal Format

This example shows a numeric value in its hexadecimal format. To do this, we use the x or X formatting specifier. The letter case of the formatting specifier determines whether the hexadecimal letters appear in lowercase or uppercase.

Sample Code 1:

            // Convert numeric value to hexadecimal value
            Console.WriteLine(string.Format("0x{0:X}", 12));
            Console.WriteLine(string.Format("0x{0:X}", 64));
            Console.WriteLine(string.Format("0x{0:X}", 123));
            Console.WriteLine(string.Format("0x{0:X}", 196));
            Console.WriteLine(string.Format("0x{0:X}", 255));
            //Output:
            //0xC
            //0x40
            //0x7B
            //0xC4
            //0xFF

If we want to convert hexadecimal value to numeric value, we can use like below:

Sample Code 2:

            //Convert hexadecimal value to numeric value
            Console.WriteLine(Convert.ToInt32("0xC", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0x40", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0x7B", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0xC4", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0xFF", 16).ToString());
            //Output:
            //12
            //64
            //123
            //196
            //255
13 Haz

C# String Formatting for DateTime

These examples shows how to format DateTime using string.Format method.

            DateTime dateTime = DateTime.Now;

            Console.WriteLine(string.Format("{0:yyyy}", dateTime));                 //2014
            Console.WriteLine(string.Format("{0:MMM dd, yyyy}", dateTime));         //Jun 13, 2014
            Console.WriteLine(string.Format("{0:ddd MM, yyyy}", dateTime));         //Fri 06, 2014
            Console.WriteLine(string.Format("{0:dddd MM, yyyy}", dateTime));        //Friday 06, 2014
            Console.WriteLine(string.Format("{0:MMM ddd dd, yyyy}", dateTime));     //Jun Cum 13, 2014
            Console.WriteLine(string.Format("{0:MMMM dddd dd, yyyy}", dateTime));   //June Cuma 13, 2014

            Console.WriteLine(string.Format("{0:yyyy-MM-dd HH:mm:ss}", dateTime));  //2014-06-13 21:05:05
            Console.WriteLine(string.Format("{0:yyyy-MM-dd HH:mm:ss}", dateTime));  //2014-06-13 21:05:05
            Console.WriteLine(string.Format("{0:MM/dd/yy H:mm:ss zzz}", dateTime)); //06.13.14 21:05:05 +03:00

The following table describes the custom date and time format specifiers.

FORMAT SPECIFIER DESCRIPTION
“d” The day of the month, from 1 through 31.
“dd” The day of the month, from 01 through 31.
“ddd” The abbreviated name of the day of the week.
“dddd” The full name of the day of the week.
“f” The tenths of a second in a date and time value.
“ff” The hundredths of a second in a date and time value.
“fff” The milliseconds in a date and time value.
“ffff” The ten thousandths of a second in a date and time value.
“fffff” The hundred thousandths of a second in a date and time value.
“ffffff” The millionths of a second in a date and time value.
“fffffff” The ten millionths of a second in a date and time value.
“F” If non-zero, the tenths of a second in a date and time value.
“FF” If non-zero, the hundredths of a second in a date and time value.
“FFF” If non-zero, the milliseconds in a date and time value.
“FFFF” If non-zero, the ten thousandths of a second in a date and time value.
“FFFFF” If non-zero, the hundred thousandths of a second in a date and time value.
“FFFFFF” If non-zero, the millionths of a second in a date and time value.
“FFFFFFF” If non-zero, the ten millionths of a second in a date and time value.
“g”, “gg” The period or era.
“h” The hour, using a 12-hour clock from 1 to 12.
“hh” The hour, using a 12-hour clock from 01 to 12.
“H” The hour, using a 24-hour clock from 0 to 23.
“HH” The hour, using a 24-hour clock from 00 to 23.
“K” Time zone information.
“m” The minute, from 0 through 59.
“mm” The minute, from 00 through 59.
“M” The month, from 1 through 12.
“MM” The month, from 01 through 12.
“MMM” The abbreviated name of the month.
“MMMM” The full name of the month.
“s” The second, from 0 through 59.
“ss” The second, from 00 through 59.
“t” The first character of the AM/PM designator.
“tt” The AM/PM designator.
“y” The year, from 0 to 99.
“yy” The year, from 00 to 99.
“yyy” The year, with a minimum of three digits.
“yyyy” The year as a four-digit number.
“yyyyy” The year as a five-digit number.
“z” Hours offset from UTC, with no leading zeros.
“zz” Hours offset from UTC, with a leading zero for a single-digit value.
“zzz” Hours and minutes offset from UTC.
“:” The time separator.
“/” The date separator.
“string”
‘string’
Literal string delimiter.
% Defines the following character as a custom format specifier.
\ The escape character.
Any other character The character is copied to the result string unchanged.
12 Haz

C# String Formatting for Double

This example converts the numeric value of this instance to its equivalent string representation, using the specified format.

C# code examples for double formatting:


        /// 
        /// String formatting for double
        /// 
        public void Test1()
        {
            // just two decimal places
            string format1 = string.Format("{0:0.00}", 123.4567);      
            string format2 = string.Format("{0:0.00}", 123.45);        
            string format3 = string.Format("{0:0.00}", 123.0);         
            // format1 = "123.46"
            // format2 = "123.45"
            // format3 = "123.00"


            // max. two decimal places
            string format4 = string.Format("{0:0.##}", 123.4567);      
            string format5 = string.Format("{0:0.##}", 123.4);         
            string format6 = string.Format("{0:0.##}", 123.0);
            // format4 = "123.46"
            // format5 = "123.4"
            // format6 = "123"

            // at least two digits before decimal point
            string format7 = string.Format("{0:00.0}", 123.4567);      
            string format8 = string.Format("{0:00.0}", 12.3456);       
            string format9 = string.Format("{0:00.0}", 1.2345);        
            string format10 = string.Format("{0:00.0}", -1.2345);
            // format7  = "123.5"
            // format8  = "12.3"
            // format9  = "01.2"
            // format10 = "-01.2"
        }

More information about string formatting:
http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx