Rounding Question

Hi all,

I am trying to achieve following

I want to round a value by passing a parameter for nearest upper rounding

value rounding result rounding result

nearest to 1 nearest to .5

0.2 1.0 0.5

0.5 1.0 0.5

0.8 1.0 1.0

1.0 1.0 1.0

1.2 2.0 1.5

1.5 2.0 1.5

1.8 2.0 2.0

Please guide me to do this.

thanks n regards

Srikant



Answer this question

Rounding Question

  • TogasPoon

    Add a module to your project, then write this:
    Module Module1
    Public Function RoundToHalf(ByVal x As Double) As Double
    Return Math.Round(x * 2) / 2
    End Function
    End Module



  • mxapacbell

    Just multiply by two, round, then divide by two.


  • HimanshuG

    Sorry I dont understand wat u mean

    can u give example pls.

    Sri.


  • HonDOn7

    That doesn't look right, try Round(x * 2) / 2


  • manyowa

    Taking the clue from nobugz I make it like this -

    for the first example

    (round(0.2 / .5) * .5) + .5 = .5 and ((round(0.2 / 1) * 1) + 1 = 1

    Thanks

    Sri.


  • BlueBeetle

    you can use the Math.Round class, which *almost* will work for you, but something to look at

    http://msdn2.microsoft.com/en-us/library/75ks3aby.aspx

     



  • Gary7

    The function you're looking for is Math.Ceiling.

    For example, Round1 for the first case, Round2 for the second:

    Module Module1
    Sub Main()
    End Sub

    Function round1(ByVal arg As Decimal) As Decimal
    Return Math.Ceiling(arg)
    End Function

    Function round2(ByVal arg As Decimal) As Decimal
    Return Math.Ceiling(arg * 2) / 2
    End Function

    End Module



  • manuel0081

     

    I hope my question is clear.

    for explanation of first line -

    when the value to round is 0.2 then I want to make it 1.0 (nearest to 1.0) or 0.5 (nearest to 0.5) depending on the parameter passed (1 or .5). Math.Round function will not help for this as per my knowledge.

    Sri.

     


  • Rounding Question