how to round up values ?

gvgbabu

Member
Joined
Sep 22, 2013
Messages
11
Programming Experience
Beginner
hi all
i want to round up values to nearest 50s.
that is if i have a value 1033 to 1050, 972 to 1000, 1239 to 1250 like that.
please guide me how to do it in vb.net

thanks in advance
gvg
 
hi all
i want to round up values to nearest 50s.
that is if i have a value 1033 to 1050, 972 to 1000, 1239 to 1250 like that.
please guide me how to do it in vb.net

thanks in advance
gvg
Simple math, you take the number you want to round and you divide it by the number you want round up to (make sure neither are 0 of course) and you take the whole number of that division and multiply it by the number you want to round up to:
    Private Function RoundNumber(Number As Integer, RoundTo As Integer) As Integer
        If Number > 0I AndAlso RoundTo > 1I Then
            Return CInt(Math.Round(Number / RoundTo, 0I) * RoundTo)
        Else
            Return Number
        End If
    End Function
 
Rounding up or down to incremental value

Code for a Console application which will give results for rounding to the nearest increment (which could be either higher or lower). Also shows results for rounding up or down using the stated increment.

VB.NET:
        Dim number, roundup, result As Integer
        Console.WriteLine("Round a whole number to the nearest desired increment")
        Console.WriteLine()
        Console.Write("Enter number to round:  ")
        Integer.TryParse(Console.ReadLine, number)
        Console.Write("Enter increment value:  ")
        Integer.TryParse(Console.ReadLine, roundup)
        Console.WriteLine()
        If number > 0 AndAlso roundup > 1 Then
            result = CInt(Math.Round(number / roundup)) * roundup
            Console.WriteLine("The nearest round number is:  " & result)
            Console.WriteLine()
            result = CInt(Math.Round((number + (roundup / 2)) / roundup)) * roundup
            Console.WriteLine("The number rounded up is:  " & result)
            Console.WriteLine()
            result = CInt(Math.Round((number - (roundup / 2)) / roundup)) * roundup
            Console.WriteLine("The number rounded down is:  " & result)
        End If
        Console.ReadLine()
 
Back
Top