var numberFormat = new NumberFormatInfo();
numberFormat.NumberDecimalSeparator = ".";
numberFormat.NumberDecimalDigits = 2;
decimal a = 10.00M;
decimal b = 10M;
Console.WriteLine(a.ToString(numberFormat));
Console.WriteLine(b.ToString(numberFormat));
Console.WriteLine(a == b ? "True": "False");
In console: 10.00 10 True
Why is it different? More important, how do I call ToString() to ensure same output no matter how a variable is initialized?
The NumberDecimalDigits
property is used with the "F"
and "N"
standard format strings, not the ToString
method called without a format string.
You can use:
Console.WriteLine(a.ToString("N", numberFormat));