Wednesday, September 1, 2010

How to add comma in a int or decimal value

This code show how to add comma into int or decimal value.Suppose we have a value 33000
and want to display this 33,000.00.

string FirstP = "";
string SecP = "";
string Finalstr = "";
int MyValue = Convert.ToInt32(Projected);
string myStr = MyValue.ToString();
int Length = myStr.Length;
string dc = ".00";

do
{
FirstP = myStr.Substring(0, Length - 3);
SecP = myStr.Substring(Length - 3, 3);
Finalstr = "," + SecP + Finalstr;
myStr = FirstP;
Length = myStr.Length;
}
while (Length > 3);

if (myStr == "")
{
Finalstr = Finalstr.Substring(1, Finalstr.Length);
}
else
{
Finalstr = myStr + Finalstr + dc;
}

lblSalary.Text = Finalstr.ToString();

0 comments:

Post a Comment