Rounding a number down in Visual Basic
Solution 1
You can do this with Math.Floor. However, you'll need to multiply * 100 and divide, since you can't supply a number of digits
Dim theNumber as Double
theNumber = 2.556
Dim theRounded = Math.Sign(theNumber) * Math.Floor(Math.Abs(theNumber) * 100) / 100.0
Solution 2
Another way to do it that doesn't rely on using the String type:
Dim numberToRound As Decimal
Dim truncatedResult As Decimal
numberToRound = 2.556
truncatedResult = (Fix(numberToRound*100))/100
Solution 3
The Math.Floor( ) answer is good. I'm not sure exactly which VB environments Fix( ) is defined in. As Justin points out, Math.Floor( ) won't work with negative numbers. You'd have to take the absolute value, then multiply by the SGN( ) of the number. I don't know the exact name of the function that you'd use to get the SiGN (not sin() ) of the number.
In pseudo-code, taking negative values into account, the result would looks like:
result = sgn( num ) * floor( abs( num * RoundToDig ) ) / RoundToDig
-- Furry cows moo and decompress.
Solution 4
To round down
Math.Floor(number)
To trim characters
number.Substring(0,1)
You can convert it to string.
Solution 5
Dim Input As Decimal
Dim Output As Decimal
Input = 2.556
Output = Input - (Input Mod 0.01)
This will work with both positive and negative numbers
Comments
-
Paul almost 2 years
I have a Visual Basic application that needs to round a number down, for example, 2.556 would become 2.55 and not 2.26.
I can do this using a function to strip off the characters more than 2 right from the decimal point using this:
Dim TheString As String TheString = 2.556 Dim thelength = Len(TheString) Dim thedecimal = InStr(TheString, ".", CompareMethod.Text) Dim Characters = thelength - (thelength - thedecimal - 2) _2DPRoundedDown = Left(TheString, Characters)
Is there a better function to do this?
-
Justin almost 15 yearsUsing Fix() will be slightly faster than using Floor().
-
Reed Copsey almost 15 yearsDo you anything that supports that statement?
-
Shvalb almost 15 yearsSometimes it's beneficial to store your numbers as integers, or fixed-point (if you don't need the full range of floating-point), performing calculations on the fixed-point numbers and then adjust them when displaying to the user.
-
Reed Copsey almost 15 years@Justin: Good point - I edited to account for negative numbers, as well as positive
-
Reed Copsey almost 15 years@Justin: Fix is actually slower than Math.Floor - it does a check, then calls Math.Floor internally. Run reflector on Microsoft.VisualBasic.dll for details.
-
Saul Dolgin almost 15 yearsI am used to the Fix() function from VB6, but it is available in VB.NET as well - msdn.microsoft.com/en-us/library/…
-
Reed Copsey almost 15 years@WyrdestGeek: The function is Math.Sign. @Saul: Fix calls Math.Floor internally.
-
Saul Dolgin almost 15 yearsWhat is this business about decompressing bovines?
-
Justin almost 15 yearsWhoops I said it backwards. Your comment was my rationale.
-
Robert L over 14 yearsSince this is VB, just use a Currency datatype.