简体   繁体   中英

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

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

The value of 'varDecimal' will keep change but in the output after decimal i only need #.0 or #.5

for example

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

The idea is after decimal above 0 should be rounded to .5 and above .5 should be rounded 1

This is what it looks like in C#, but it should be easy to port to VB.Net:

http://goo.gl/ztLJC2

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

You could try using 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

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

这应该工作:math.round((value * 2)+0.5)/ 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM