1

Possible Duplicate:
.NET String.Format() to add commas in thousands place for a number

i have a double value that i want to add it thousand separator, with below conditions :
1- remove zero decimals after dor(.)
2-control count of decimals

for example :

string str_Money = Convert.ToDouble(Money).ToString("N3");

this code for Money = 50000 returns 50,000.000, but i don't want zero decimals(mean i want 50,000)
another example : for Money = 50000.2355 returns 50,000.235 and that is exactly what i want
how can i reformat it?

Community
  • 1
  • 1
SilverLight
  • 19,668
  • 65
  • 192
  • 300

1 Answers1

1

Using the accepted answer from .NET String.Format() to add commas in thousands place for a number, use an if statement to control to returned format.

string str_Money = "";
if (money % != 0) // test for decimals
{
    str_Money = string.Format("{0:n0}", money); // no decimals.
}
else
{
    str_Money = string.Format("{0:n}", money);
}
Community
  • 1
  • 1
jac
  • 9,666
  • 2
  • 34
  • 63