ROUNDUP Function??

Shortie

Member
Joined
Oct 20, 2005
Messages
5
Location
London
Programming Experience
Beginner
ROUNDUP Function?? [RESOLVED]

Hi

In Excel, there is a function ROUNDUP(). Is there something similar in VB.NET? I know I can write a function to handle this using convoluted IF statements, but I can't seem to find anything that straightforwards.

Thanks.
Shortie.
 
Last edited:
Hey, I can't take credit for this but it's the one I use, can't remember where I got it! It will always round up if you set the RoundUP parameter to true.

VB.NET:
Public Function RoundToValue(ByVal nValue As Object, ByVal nCeiling As Double, Optional ByVal RoundUp As Boolean = True) As Double

        Dim tmp As Integer
        Dim tmpVal
        If Not IsNumeric(nValue) Then Exit Function
        nValue = CDbl(nValue)

        'Round up to a whole integer -
        'Any decimal value will force a round to the next integer.
        'i.e. 0.01 = 1 or 0.8 = 1

        tmpVal = ((nValue / nCeiling) + (-0.5 + (RoundUp And 1)))
        tmp = Fix(tmpVal)
        tmpVal = CInt((tmpVal - tmp) * 10 ^ 0)
        nValue = tmp + tmpVal / 10 ^ 0

        'Multiply by ceiling value to set RoundtoValue
        RoundToValue = nValue * nCeiling

    End Function

Hope this helps.. I don't think there is a built in RoundUP function.
 
Hey Kulrom,

Will the Math.Round function always round up like the access ROUNDUP function or does it round to the nearest in both directions? I'll be most annoyed if it does always round up as I spent ages trying to work this one out at some point in my Vb.NET past!!
 
give it a try ;) JK


Well, the Round method rounds a number to the nearest whole number. For example, the following code is used to round the number 3.4677789:
answer = Math.Round(3.4677789)​
In this case, the answer would be 3. If you use Round to round a number that is exactly halfway between two numbers (for example, 3.5), then Round always returns the even number closest to the number. Thus, in this case, Math.Round(3.5) would be 4. However, Math.Round(6.5) would return 6. The Floor method is used to truncate a real number. Floor actually returns the largest whole number smaller than the original number. For example, use the following to return the floor of 5.9:
answer = Math.Floor(5.9)​
In this case, the answer would be 5. Note that the Floor method may behave differently than you might expect for negative values. For example, Math.Floor(-5.9) returns -6.


Anyway, notice that Math class contains a number of additional methods for making trigonometric and logarithmic calculations.

HTH
Regards ;)
 
Math.Round uses statistical rounding to the nearest whole numer:
1.5 rounds to 2
2.5 rounds to 2
3.5 rounds to 4
4.5 rounds to 4

Math.Ceiling always rounds UP to the nearest whole number
Math.Floor always rounds DOWN to the nearest whole number

also if you dont want to use .Floor just do: Math.Round(yourNumber - .599999)
also if you dont want to use .Ceiling just do: Math.Round(yourNumber + .599999)
 
Back
Top