Can anyone help me get math.round () to get the following result

DecimalValue = (Math.Round (varDecimal / 8, 1)

The 'varDecimal' value will save the changes, but in the output after the decimal value I only need # .0 or # .5

eg

9/8 = 1.1 -> but I need it 1.5
11/8 = 1.4 -> but I need it 1.5
21/8 = 2.6 -> but I need this 3.0
27/8 = 3.4 -> but I need it 3.5
33/8 = 4.1 -> but I need it 4.5
39/8 = 4.9 -> but I need it 5.0
45/8 = 5.6 -> but I need it 6.0

Idea after decimal number above 0 should be rounded to 0.5 and above. 5 should be rounded to 1

+3


source to share


4 answers


This is what it looks like in C #, but needs to be easily portable to VB.Net:

http://goo.gl/ztLJC2



Math.Ceiling((double)value * 2) / 2 

      

+4


source


You can try this tiny method.



Public Function Round(num As Double) As Double
    Dim intVal =  CInt(Math.Truncate(num))
    Dim remainder = num - intVal

    If remainder = 0 Then
        Return num

    ElseIf remainder <= 0.5 Then

        remainder = 0.5


        Return (intVal + remainder)
    Else


        intVal += 1


        Return CDbl(intVal)
    End If

End Function

      

+1


source


Use Math.Ceiling()

Math.Ceiling(value)
//Do your computations to get the .5

      

https://msdn.microsoft.com/en-us/library/zx4t0t48(v=vs.110).aspx

0


source


this should work: math.round ((value * 2) +0.5) / 2)

-2


source







All Articles