Different formats with String.Format in C#

Peermohamedmydeen
Posted by Peermohamedmydeen under C# category on | Points: 40 | Views : 2770
We can format an integer into different string formats as below. This formats will be helpful to you, when you work with currencies, Hexadecimal and Exponential representations.

The Snippet below.

int nVal = 12345678;
string sVal = string.Format("{0:N}",nVal);
Console.WriteLine(sVal); // Output - 12,345,678.00 Numeric

sVal = string.Format("{0:C}",nVal);
Console.WriteLine(sVal); // Output - $12,345,678.00 Numeric with currency Symbol

sVal = string.Format("{0:E}",nVal);
Console.WriteLine(sVal); // Output - 1.2345678E+007 Exponential

sVal = string.Format("{0:X}",nVal);
Console.WriteLine(sVal); // Output - BC614E HexaDecimal



I hope it will be helpful

Thanks
PMM :)

Comments or Responses

Login to post response