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);
// Result if current culture is en-US:
// The current price is $17.37 per ounce.
}
}
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);
}
}
Output :
string = 731%
Example 3,
decimal num = 80.7856559m;
Console.WriteLine("Format Decimal: {0:n2}", num);
Console.WriteLine("Format Decimal: {0}", num);
}
}
Output :
Format Decimal: 80.79