Converting to two decimal places

I have following C# code:

Total = SubTotal - (SubTotal*ColumnPercent/100);

All the value are initially declared as “Decimal”. I want the Total to store values with two decimal places.

For example, if the calculated value comes out to be: 2155.33333, Total must store: 2155.33

If it’s for currency try using:

Console.WriteLine("{0:c}", Total);

for a console app or

String.Format("{0:c}", Total);

for a web form.

If not for currency … umm, I forget. BRB. :slight_smile:


static void Main(string[] args)
	Double Total;
	Double SubTotal = 100;
	Double ColumnPercent = 33.3333333333333;

	Total = SubTotal - (SubTotal * ColumnPercent/100);

	Console.WriteLine("{0}", Total);
	Console.WriteLine("{0:f2}", Total);


By the way “f” is the "fixed-point string format specifier. I believe 2 is the default so you may be able to leave it out and just use {0:f}.

Though not as efficient as string.format, you can also use the FormatNumber function. That’s a throwback to the days of ASP.OLD :slight_smile: