How to round a number to a multiple of 8 in VB.NET

2 Answers

0 votes
Imports System

Public Class Program
    Private Shared Function roundToMultipleOf(ByVal number As Integer, ByVal roundTo As Integer) As Integer
        Return (number + (roundTo - 1)) And Not (roundTo - 1)
    End Function

    Public Shared Sub Main(ByVal args As String())
        Console.WriteLine(roundToMultipleOf(9, 8))
        Console.WriteLine(roundToMultipleOf(19, 8))
        Console.WriteLine(roundToMultipleOf(71, 8))
    End Sub
End Class



  
  
' run:
'
' 16
' 24
' 72
'


 



answered Jun 8, 2024 by avibootz
0 votes
Imports System

Public Class Program
    Public Shared Function roundToMultipleOf(ByVal number As Double, ByVal multipleOf As Double) As Double
        Return multipleOf * CLng(Math.Round(number / multipleOf, MidpointRounding.AwayFromZero))
    End Function

    Public Shared Sub Main(ByVal args As String())
        Console.WriteLine(roundToMultipleOf(9, 8))
        Console.WriteLine(roundToMultipleOf(19, 8))
        Console.WriteLine(roundToMultipleOf(71, 8))
    End Sub
End Class


  
  
' run:
'
' 8
' 16
' 72
'



answered Jun 8, 2024 by avibootz

Related questions

1 answer 94 views
2 answers 128 views
2 answers 137 views
2 answers 122 views
1 answer 130 views
2 answers 165 views
2 answers 119 views
...