It’s was told to me that this can never be shown to the client and further more the number can never be rounded (anyone ever see Office Space… sound familiar?).
So I went searching on how to do this in .NET. After many, many hours of searching and testing, I could not find any way in .NET to do this. Every function I found would always round the number! I found myself wishing for just a way to flip a switch to tell .NET to stop doing that. So I finally gave up and came up with a short function that will do what the client needs.
The ConvertDecimal function will simply return a decimal number with the desired decimal places with NO ROUNDING.
Shared Function ConvertDecimal(ByVal inputValue As Decimal, ByVal placesAfter As Integer) As Decimal
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
Dim tempValue As String = Convert.ToString(inputValue)
Dim tempSplit() As String = tempValue.Split(Convert.ToChar("."))
If (tempSplit.GetLowerBound(0) = 0) Then
sb.Append(tempSplit(0))
End If
If (tempSplit.GetUpperBound(0) = 1) Then
sb.Append(".")
sb.Append(tempSplit(1).Substring(0, placesAfter))
End If
Return Convert.ToDecimal(sb.ToString())
End Function
Tip Submitted By: David McCarter
This code can be found in the open source dotNetTips.Utility assembly
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.
