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: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
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
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()