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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determing even/odd values

Status
Not open for further replies.

KM8888

Programmer
Nov 21, 2011
69
US
Hi everyone, I was wondering how to find if a number is even/odd but with a slight twist...

I have the code build in to determine so it sets the number to a variable then runs the following.

Dim i as Integer

If i Mod 2 Then
MsgBox "Number is Odd"
Else
MsgBox "Number is Even"
End If


The twist I am trying to incorporate is finding if the number including the decimals are odd/even. So for example 5.19 I would want to prompt as odd and 5.20 I would want to prompt as even...

Is there a way to do this? After trying a few things it only round up or down to the base number and says odd and even for that number only. Would I need to develop a work around so it only grabs the last two numbers?

The reason I haven't already done this is because I am finding out if a in script calculated value is even/odd so that value doesn't exist on my screen.

Thanks for any insight!

 
Thanks to flexibility of VB:
Code:
If Right(i,1) Mod 2 Then
    MsgBox "Number is Odd"
Else
    MsgBox "Number is Even"
End If



combo
 
Thank you combo that worked well, I have a related kind of problem...

It seems using PCOMM or maybe vb as a whole the round functions seem to round downwards. So if I have a number like 59.255 It seems to always round to 59.25 and not 59.26.

I have looked all over and only found functions that are not useable for me in the PCOMM application, so I was just wondering if there was any insight on this... I found things like RoundUp and Ceiling but neither can be used in my application so I'm not sure if there is a work around or another hidden function I haven't been privy to.

I'll build a little sample to make it easier so say I had

Dim Value

Value = ?(number, 2)

Right now I have the "Fix" function in where the ? mark is and it gives me my value of 59.25, is there any way I can get the 59.26 value?

Thanks for any insight everyone!

 

???
Code:
    Dim i
    
    i = 59.255
    
    If Right(i, 1) Mod 2 Then
        MsgBox "Number is Odd"
    Else
        MsgBox "Number is Even"
    End If
ODD!!!

Why would you use INT or FIX???

Have you read VBA Help on FIX???

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


How about this...
Code:
    Dim i, s As Integer
    
    i = 59.255
    
    s = Right(i, 1)
    
    If Int(s / 2) <> s / 2 Then
        MsgBox "Number is Odd"
    Else
        MsgBox "Number is Even"
    End If


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
> vb as a whole the round functions seem to round downwards

Not quite true. VBA uses bankers rounding. This article explains the different forms of rounding available in Microsoft products (and a little explanation of why each form of rounding exists - in VBA's case bankers rounding was chosen to avoid bias in statistical analysis), and also a library of VBA functions to implement alternative roundings to those available in your product.
 
Thanks for the responses, I'll try your suggestion tomorrow Skip. I was only using Fix when I did want to round down but since I now wanted to round up I couldn't find a way to do that, as strongm explained it's because of their bankers rounding kind of set up. I was just inquiring if there was a way around their bankers rounding but I'll try your suggestion and get back to you, thanks!
 
If your host application is excel, it is possible to round with worksheet function (5s always up):
[tt]MsgBox Application.WorksheetFunction.Round(59.255, 2)[/tt]

combo
 
Unfortunately I am using Personal Communications and not excel so I don't have access to some of the same functions to round up. In my PCOMM session a Round(59.255,2) seems to always produce 59.25 as well because of their bankers rounding methods
 


I have had a similar problem using VB from other non-Microsoft applications, like Attachmate Extra.

However, since I virtually ALWAYS want to get my data into Excel, or I have a list of data from Excel, I find it much MUCH easier to code in Excel VBA, get my OTHER application object and drive in my Lamborghini (Excel) in comfort, rather than climbing into the Yugo (Extra) and getting bounced around and sweating.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Just use the SymUp (or possibly AsymUp) function from the link I gave you
 
Hey strongm, I'm having a little trouble getting those functions to work with PCOMM. I am getting syntax error problems when trying to use some of the code and it might be also because I am fairly novice with this stuff that I cannot get it to work.

Using AsymUp or SymUp on their own prompts a variable definition problem, trying to set it up as a function prompts all different types of syntax errors.

Ideally a code like

AsymUp(59.255, 2)

would produce > 59.26 but it doesn't seem like its that simple. I'm not really sure how to set up those custom functions to work as PCOMM seems to not recognize it... Again my amateurness doesn't really help this cause but I seem to be just struggling a bit with this, I do appreciate all the help thus far though!
 
My current solution is something such as this...

Dim sValue

sValue = Round(sValue + 0.001, 2)

Seems to be the only way I can really get it to round up when it is a number like 59.255.

Perhaps not the best solution just thought I'd share it as to what I have so far :S
 
>I am getting syntax error problems

I'm not particualrly familaiar with PCOMM. Are we talking about IBM Personal Communication software? Because if so, I believe the built-in language is not VBA, it is VBScript. And these are different beasts.
 
Yeah I apologize, that is what I mean by PCOMM. I thought this was the most appropriate forum for those related questions but is one of the others better? (I mean on this site). I have just found this website the most helpful but wasn't too sure where to direct those types of questions. There doesn't seem to be a lot of experts I can find that run PCOMM but I probably don't look in all the right places. Regardless I still find a lot of input helpful on here, VBA seems quite similar to VB Script though you are right there are some important differences.
 
>there are some important differences.

There are some quite significant differences! In this particular case (for the suggested functions) you are dealing with the fact that VBscript does not have any typing, does not support optional parameters and does not have the IIF statement. They would have to be rewritten soomething like this:
Code:
[blue]Function AsymUp(X, Factor)
    Dim Temp
    Factor = 10 ^ Factor
    Temp = Int(X * Factor)
    AsymUp = (Temp + 1 + (X = Temp)) / Factor
End Function

Function SymUp(X, Factor)
    Dim Temp
    Factor = 10 ^ Factor
    Temp = Fix(X * Factor)
    SymUp = (Temp + (1 + (X = Temp)) * Sgn(X)) / Factor
End Function
[/blue][code]
(I've also slightly modified the functions so that the second parameter specifies the number of digits after the decimal as per the normal Round function, rather than the somewhat esoteric scaling done in the original functions. Also you should note that if you ask for more decimal places than exist in the original number inaccuracy will creep in)
 
It looks good, and it's probably my amateur nature that I can't really apply it to the personal communications system, whenever I try and use the code I get a syntax error that highlights the first function line so I'm not really sure how functions are used within Personal Communications but it seems they have some issues with it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top