Part 28 String.formate()

String.formate() :  if you want to insert the  value of an object or variable into another string etc.


Example 1.


class Program

    {       

        {
            Decimal pricePerOunce = 17.3654m;
            String s = String.Format("The current price is {0:C2} per ounce.", pricePerOunce);
            Console.WriteLine(s);
            // Result if current culture is en-US:
            //      The current price is $17.37 per ounce.
            Console.ReadLine();
        }
    }

Output :
The current price is ? 17.37 per ounce.

Example 2,
we can specify that a value (like a double ex:-double data= 0.7343;) can be formatted inside string.Format. This format string in this example uses the 0:0.1% syntax. This means that the second argument is formatted with the pattern 0.1%.The "0.1%" part specifies the number of digits.


 class Program

    {       

        {
            double data= 0.7343;
            String result = string.Format("string = {0:0.1%}", data);
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }

Output :
string = 731%

Example 3,


 class Program

    {       

        {


             decimal num = 80.7856559m;

             Console.WriteLine("Format Decimal: {0:n2}", num);

             Console.WriteLine("Format Decimal: {0}", num);
             Console.ReadLine();
        }
    }

Output :


Format Decimal: 80.79