I have decimals values and want to show them without values after the decimal point. For example
10.00 => 10.
I already have N2 formatting.
myValue.ToString("N2");
Is there a simple way to combine those two?
I have decimals values and want to show them without values after the decimal point. For example
10.00 => 10.
I already have N2 formatting.
myValue.ToString("N2");
Is there a simple way to combine those two?
You can use a 0 placeholder followed by a period literal inserted into the format string. To insert a literal, escape it with a backslash. Note that a single backslash is already an escape for string literals, so you actually need two, or you can use a verbatim string, as in this example:
var d = 10.00M;
Console.WriteLine( d.ToString(@"0\.") ); //Outputs "10."
If you are worried about internationalization, don't use a period constant; use a decimal separator from the current culture.
string decimalSymbol = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
Console.WriteLine( d.ToString(@"\" + decimalSymbol) ); //Outputs "10." or "10,"