Monday, March 26, 2012

Working with Decimal

Hi

How to return a Decimal with only 2 decimals, code bellow:

Public

Function GetShoppingCartTotal(ByVal cartIDAsString)AsDecimal' Create Instance of Connection and Command ObjectDim myConnectionAs SqlConnection =New SqlConnection(Class_MM_Const.connectionString)Dim myCommandAs SqlCommand =New SqlCommand("MM_SP_GetShoppingCartTotal", myConnection)' Mark the Command as a SPROC

myCommand.CommandType = CommandType.StoredProcedure

' Add Parameters to SPROCDim parameterCartIDAs SqlParameter =New SqlParameter("@dotnet.itags.org.CartID", SqlDbType.NVarChar, 50)

parameterCartID.Value = cartID

myCommand.Parameters.Add(parameterCartID)

Dim parameterTotalCostAs SqlParameter =New SqlParameter("@dotnet.itags.org.TotalCost", SqlDbType.Money, 8)

parameterTotalCost.Direction = ParameterDirection.Output

myCommand.Parameters.Add(parameterTotalCost)

' Open the connection and execute the Command

myConnection.Open()

myCommand.ExecuteNonQuery()

myConnection.Close()

' Return the TotalIf parameterTotalCost.Value.ToString() <> ""ThenReturnCType(parameterTotalCost.Value,Decimal)ElseReturn 0EndIfEndFunction

You can use the static method Math.Round to round a decimal off to the nearest hundreth:

Dim numberAs Decimal = 4.44578Dim roundedAs Decimal = Math.Round(number, 2, MidpointRounding.ToEven)
See this URL for more information:http://msdn2.microsoft.com/en-us/library/f5898377.aspx

0 comments:

Post a Comment