Wednesday, November 7, 2007

Rounding a calculated value in .net

I have had to round a calculated value before it is presented in the display. This is what I did to accomplish this task.

Use the function System.Math.Round(value, precision) where Value is the number that needs to be rounded to precision number of digits.

The other option is to set the control's DataFormatString to {0:n2} where n denotes a float or double value and the number 2 denotes the precision that is required. Don't forget to set HtmlEncode to false!

Though both methods work great, I decided to go with the later option as I would be able to delay the rounding until it is actually ready to be displayed.

No comments: