|
-
Dec 24th, 2014, 03:53 PM
#1
Thread Starter
Junior Member
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
-----------------------------
-
Dec 24th, 2014, 04:08 PM
#2
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
-
Dec 24th, 2014, 05:02 PM
#3
Thread Starter
Junior Member
Re: Rounding issue
 Originally Posted by DataMiser
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|