Rounding a number down in VB?

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.

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.

This won't work if the number is negative. – Justin Aug 4 '09 at 16:33 Sometimes 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. – David Smith Aug 4 '09 at 16:45 @Justin: Good point - I edited to account for negative numbers, as well as positive – Reed Copsey Aug 4 '09 at 16:47 Since this is VB, just use a Currency datatype.

– Robert L Aug 4 '09 at 9:44.

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.

Using Fix() will be slightly faster than using Floor(). – Justin Aug 4 '09 at 16:27 1 Do you anything that supports that statement? – Reed Copsey Aug 4 '09 at 16:37 1 @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. – Reed Copsey Aug 4 '09 at 16:51 Whoops I said it backwards. Your comment was my rationale.

– Justin Aug 4 '09 at 17:25.

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.

I am used to the Fix() function from VB6, but it is available in VB. NET as well - msdn.microsoft. Com/en-us/library/… – Saul Dolgin Aug 4 '09 at 16:51 @WyrdestGeek: The function is Math.Sign.

@Saul: Fix calls Math. Floor internally. – Reed Copsey Aug 4 '09 at 17:00 What is this business about decompressing bovines?

– Saul Dolgin Aug 4 '09 at 17:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions