Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only Allow Even Numbers, Round to .5 etc 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a program that only allows numbers following the rules as below...

ie 1st line of rule....and number between 2 and 2.99 can only bo 2.02 2.04 2.06 2.08 2.10 2.12 ...... 2.98 etc
ie last line only allows 110 120 130 140 .........etc
+ lines as well

Would appreciate some help ..Is there an easy way to carry out this type of thing
it's got me beaten...@!@!@!!@

Regards TROBT



'From 2.02 to 3.00 one tick is equal to 0.02.
'From 3.05 to 4.00 one tick is equal to 0.05.
'From 4.1 to 6.0 one tick is equal to 0.1.
'From 6.2 to 10.0 one tick is equal to 0.2.
'From 10.5 to 20.0 one tick is equal to 0.5.
'From 21 to 30 one tick is equal to 1.
'From 32 to 50 one tick is equal to 2.
'From 55 to 100 one tick is equal to 5.
'From 110 to 1000 one tick is equal to 10.
 
Hi,

What's the rule for numbers less than 2 or greater than 1000?

I'd probably construct a table for these rules.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
You could try the following simple function. Note it uses VB's Round function, which performs Banker's rounding. This may not be what you want.

Code:
[blue]Private Function TickRound(myNum As Double) As Double
    Dim tick As Double
    
    If myNum > 2 Then tick = 0.02
    If myNum > 3 Then tick = 0.05
    If myNum > 4 Then tick = 0.1
    If myNum > 6 Then tick = 0.2
    If myNum > 10 Then tick = 0.5
    If myNum > 20 Then tick = 1
    If myNum > 30 Then tick = 2
    If myNum > 50 Then tick = 5
    If myNum > 100 Then tick = 10
    
    tick = 1 / tick
    
    TickRound = Round(myNum * tick) / tick
End Function[/blue]
 
What happens to other values outside the definition like values between 100 and 110 or values between 50 and 55 just to point out two very obvious gaps?

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
In this case anything larger than 50 but less than 100 would get tick = 5.

But don't you wanna do something with 51 to 54?

"Trying is the first step to failure..." - Homer
 
Don't believe the gaps you identify actually exist. The rules in the OP (those after the sig) are expressed 'wrongly' - in the sense that the first value of the interval appears to be the expected rounded up OUTPUT value - i.e a real value of 100 would be rounded up to the next tick of 110, so the range starts at 110.

Mind you the Banker's rounding in my code does mean we don't meet this interpretation. So, the following minor update shoukd work. Note also that this is simply an illustrative solution; the numbers representing where the intervals can obviously be changed to whatever you like.

Code:
[blue]Private Function TickRound(myNum As Double) As Double
    Dim tick As Double
    
    If myNum >= 2 Then tick = 0.02
    If myNum >= 3 Then tick = 0.05
    If myNum >= 4 Then tick = 0.1
    If myNum >= 6 Then tick = 0.2
    If myNum >= 10 Then tick = 0.5
    If myNum >= 20 Then tick = 1
    If myNum >= 30 Then tick = 2
    If myNum >= 50 Then tick = 5
    If myNum >= 100 Then tick = 10
    
    tick = 1 / tick
    
    ' Rounding Up
    TickRound = (Int((myNum * tick)) / tick) + (1 / tick)
End Function[/blue]
 

Strongm..

Once again thanks for your support....

I had tried ... Round(myNum * tick) / tick ...but realised it did not work in all cases

Your extra refinement seems to have done the trick..

Thanks again ,much appreciated

Robert

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top