Wednesday 27 November 2013

C# - How do I round a decimal value to 2 or more decimal places (for output on a page?)



In this article I will explain how I do round a decimal value to 2 or more decimal places.

Do round a decimal value to 2

string PART= Convert.ToDecimal(dtList.Tables[0].Rows[dr]["value"]).ToString("#.##");

If you have 0.00 values, its returns empty. To overcome this problem writes below syntax.

 string PART= Convert.ToDecimal(dtList.Tables[0].Rows[dr]["value"]).ToString("0.##");

Do round a decimal value to more than 2

string PART= Convert.ToDecimal(dtList.Tables[0].Rows[dr]["value"]).ToString("0.###");

string PART= Convert.ToDecimal(dtList.Tables[0].Rows[dr]["value"]).ToString("0.####");

No comments:

Post a Comment