Prevent Math.Round(95.55555555,2) from rounding to 95.56 in VB.NET
If I do Math.Round(95.55555555,2)
in VB.NET, the result is 95.56
, but I want it the result the be 95.55
. Is there a way to do this in VB.NET? I guess I just want to ke开发者_如何转开发ep the decimal places, but not round them
Looks like Math.Truncate(95.5555555 * 100) / 100
.
See Truncate Decimal number not Round Off
Try using Math.Floor(95.55555555 * 100) / 100
Or, if you want to round to a specific number of decimals:
Public Function RoundDown(ByVal value As Double, ByVal decimalPlaces As Integer) As Double
If (decimalPlaces < 1) Then
Throw New ArgumentException("Invalid decimalPlaces: less than 1")
EndIf
Dim factor As Integer = 10 ^ decimalPlaces
Return Math.Floor(value * factor) / factor
End Sub
There are several ways to do this. One would be to subtract 0.05 from the number then use Math.Round(number, 2)
. (This works on the same principle as implementing floor
and ceiling
functions when all you have is round
.)
A better way is probably
Math.Truncate(number * 100) / 100
That just multiplies the number by 100 and truncates it, giving you an integer value with the digits you want, then divides by 100 to turn it back to a decimal.
You don't want Math.Round. You want Math.Truncate.
Dim decimalNumber As Double = 95.55555555
Dim truncatedNumber As Double = Math.Truncate(decimalNumber * 100) / 100
Your result will be 95.55.
You can use this:
static double TruncateWithDecimals(double n, int nOfDec)
{
return Math.Round(Math.Floor(n * Math.Pow(10, nOfDec)) / Math.Pow(10, nOfDec), nOfDec);
}
sorry this is C#, but you can easily guess how to translate in vb I think.
Public Function Round(Number As Decimal, places As Integer) As Decimal
'Convert number to string
Dim NumberString As String = Number.ToString
'Check if the number contains a decimal, if not return the number
If NumberString.IndexOf("."c) = -1 Then Return Number
'Get the whole number part of the string
Dim IntegerPart As String = NumberString.Split("."c)(0)
'Get the Decimal part of the string
Dim DecimalPart As String = NumberString.Split("."c)(1)
'If the number is already rounded to n decimal places, then return the number
If DecimalPart.Length = places Then Return Number
'Get whichever decimals are being rounded to
Dim ToPlacePart As String = DecimalPart.Substring(0, places)
'get the other part that will be compared
Dim ComparePart As String = DecimalPart.Substring(places)
'Create a midpoint to compare the compare part to
Dim ControlMidPoint As Decimal = Decimal.Parse("1" & Replace(Space(ComparePart.Length), Space(1), "0")) / 2
'Create the base result(Add the integer part to the decimal part that will stay)
Dim Result As Decimal = Decimal.Parse(IntegerPart & "." & ToPlacePart)
'Create an increment to add if the comparepart is greater than the mid point(ex 0.001, 0.01, 0.0000001)
Dim AddNum As Decimal = Decimal.Parse("0." & Replace(Space(ToPlacePart.Count - 1), Space(1), "0") & "1")
'If the comparepart was equal to or greater than the midpoint, then add the addpart to the base result and return it
If Decimal.Parse(ComparePart) >= ControlMidPoint Then Return Result + AddNum
'Just return the base result, because the compare part was smaller than the midpoint
Return Result
End Function
精彩评论