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 the result to be 95.55 . Is there a way to do this in VB.NET? I think I just want to keep the decimals, but not round them

+4
source share
6 answers

Similar to Math.Truncate(95.5555555 * 100) / 100 .
See Reduce decimal without rounding.

+7
source

Try using Math.Floor(95.55555555 * 100) / 100

Or, if you want to round to a certain number of decimal places:

 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 
+5
source

There are several ways to do this. One could subtract 0.05 from the number, then use Math.Round(number, 2) . (This works on the same principle as the floor and ceiling functions, when all you have is round .)

Best probably

 Math.Truncate(number * 100) / 100 

It just multiplies the number by 100 and truncates it, giving you an integer value with the numbers you want, and then divides by 100 to return it back to decimal.

+2
source

You do not 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.

+1
source

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 to vb, I think.

0
source
 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 
0
source

Source: https://habr.com/ru/post/1345906/


All Articles