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.

How to determine if a data value is null

When we try to get data using a series of SQL joins, at some point, we are bound to hit a data element with a Null value. It took me a little bit of time the first time i came across such an error. This is how i dealt with it:


If TypeOf (MyDs.Tables(0).Rows(j).Item(3)) Is System.DBNull Then
' do something here

End If