Results 1 to 3 of 3

Thread: Rounding issue

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Location
    Malta
    Posts
    31

    Angry Rounding issue

    I am trying to round up numbers in a legacy application which I had coded in VB6 to obtain the following outcomes:

    2.53 should be 2.60

    2.55 should be 2.60

    2.56 should be 2.60

    2.50 should remain 2.50

    I have tried to use the suggested User-Defined Rounding functions by Microsoft http://support.microsoft.com/kb/196652/en-gb

    The closest I arrived to is the Asymup function


    Code:
    Function AsymUp(ByVal X As Double, _  
    Optional ByVal Factor As Double = 1) As Double
    Dim Temp As Double
    Temp = Int(X * Factor)
    AsymUp = (Temp + IIf(X = Temp, 0, 1)) / Factor
    End Function
    I am testing this procedure by calling the function as follows:
    Code:
    Text1.Text = AsymUp(Val(Text1.Text), 10)
    But this is not producing the desired results because 2.60 for example becomes 2.7 when I want it to remain 2.6. Strangely enough 2.0 also becomes 2.1, implying that the function is not working well.

    How can I correct this to acheive the desired results
    ----------------------------
    R Cordina
    -----------------------------

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Rounding issue

    Well in your Iff statement you are comparing x to temp and adding 1 if they are not the same
    The problem with that is that on the line before you set temp=x*factor so unless factor is 1 they will not be the same and it will add 1
    Try this instead
    Code:
    AsymUp = (Temp + IIf((X*Factor) = Temp, 0, 1)) / Factor

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Location
    Malta
    Posts
    31

    Re: Rounding issue

    Quote Originally Posted by DataMiser View Post
    Well in your Iff statement you are comparing x to temp and adding 1 if they are not the same
    The problem with that is that on the line before you set temp=x*factor so unless factor is 1 they will not be the same and it will add 1
    Try this instead
    Code:
    AsymUp = (Temp + IIf((X*Factor) = Temp, 0, 1)) / Factor
    Seems to work fine now! I am doing some testing but.
    ----------------------------
    R Cordina
    -----------------------------

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width