×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Only Allow Even Numbers, Round to .5 etc

## Only Allow Even Numbers, Round to .5 etc

(OP)
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.

### RE: Only Allow Even Numbers, Round to .5 etc

Hi,

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

I'd probably construct a table for these rules.

Skip,

Just traded in my OLD subtlety...
for a NUance!

### RE: Only Allow Even Numbers, Round to .5 etc

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

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 

### RE: Only Allow Even Numbers, Round to .5 etc

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,

Just traded in my OLD subtlety...
for a NUance!

### RE: Only Allow Even Numbers, Round to .5 etc

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

### RE: Only Allow Even Numbers, Round to .5 etc

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

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 

### RE: Only Allow Even Numbers, Round to .5 etc

(OP)

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

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!