Two Decimal places using c#

72

decimal Debitvalue = 1156.547m;

decimal DEBITAMT = Convert.ToDecimal(string.Format("{0:0.00}", Debitvalue));

I have to get only two decimal places but by using this code I am getting 1156.547. Let me know which format I have to use to display two decimal places.

This question is tagged with c# formatting

~ Asked on 2012-05-25 06:25:47

The Best Answer is


68

Your question is asking to display two decimal places. Using the following String.format will help:

String.Format("{0:.##}", Debitvalue)

this will display then number with up to two decimal places(e.g. 2.10 would be shown as 2.1 ).

Use "{0:.00}", if you want always show two decimal places(e.g. 2.10 would be shown as 2.10 )

Or if you want the currency symbol displayed use the following:

String.Format("{0:C}", Debitvalue)

~ Answered on 2012-11-13 15:43:18


51

Use Math.Round() for rounding to two decimal places

decimal DEBITAMT = Math.Round(1156.547m, 2);

~ Answered on 2012-05-25 06:26:58


Most Viewed Questions: