In console application I've created 2 decimals:
I've set my culture to "en-GB"
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");
Decimal money = 14343431.948M;
Decimal moneyConversion = decimal.Parse("-34,555.897");
string decimalString = money.ToString("N3");
string moneyConversionString = moneyConversion.ToString("N3");
Console.WriteLine("Decimal value: " + decimalString); //prints 14,343,431.948
Console.WriteLine("Decimal value Converted: " + moneyConversion); //-34555.897
The first writeline shows the decimal representation as expected while the second prints -34555.897 but I was expecting -34,555.897, I'm missing the comma that separates the thousands. How come?
This is the problem:
Console.WriteLine("Decimal value Converted: " + moneyConversion); //-34555.897
You're using moneyConversion
(the decimal
value) rather than moneyConversionString
. If you change it to:
Console.WriteLine("Decimal value Converted: " + moneyConversionString);
you'll get the formatting you expect. Otherwise you're just effectively calling moneyConversion.ToString()
which will use the default "G" format.
See more on this question at Stackoverflow