Question

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?

Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top