Results 1 to 11 of 11
Thread: Integer or Real number

010204, 11:13 #1Senior Member
 Join Date
 Nov 2002
 Location
 Norway
 Posts
 239
Unanswered: Integer or Real number
Can anyone tell me how to identify whether a result of a mathematic expression is a Integer or a real number ?
Thanks

010204, 11:19 #2Grand Poobah
 Join Date
 Sep 2003
 Location
 MI
 Posts
 3,713
That depends ... Do you know the construct of the mathematical expression itself?

010204, 11:25 #3Senior Member
 Join Date
 Nov 2002
 Location
 Norway
 Posts
 239
Yes.
I want to use the result of my expression to display a message depending on the result.
If Integer "OK", if real "Not OK"

010204, 13:08 #4Cavalier King Charles
 Join Date
 Dec 2002
 Location
 Préverenges, Switzerland
 Posts
 3,740
do you want a mathematical integer or something pretty close?
maybe....
if yourNumber mod 1 = 0 then
'yourNumber feels like an integer
endif
but i don't know how this will play with floats and precision limits etc
12.000000......0000000 'is this an "integer" for you???
or
12.000000......0000001 'is this just a precision problem?
izycurrently using SS 2008R2

010204, 13:16 #5Grand Poobah
 Join Date
 Sep 2003
 Location
 MI
 Posts
 3,713
This is a real problem ... at what point is the cutoff to determine if real or int? 12 dec places? 20? 100? 1000? What kind of numbers are being used in this expression? Are there any functions (sin, cos, tan, sqrt, etc...)? Is the expression simple (r / w + y z * g )?

010204, 14:09 #6Senior Member
 Join Date
 Nov 2002
 Location
 Norway
 Posts
 239
I need this function in order to find out if a given number is dividable with another.
(of course it's dividable, but you know what I'm looking for)
Test_Answer=1,000000000000000000000000000000001 is anyhow a REAL figure, no matter how many thousand zeros coming up.
Test_Answer=1 is INTEGER
I used this function with great success:
If (Right(Test_Answer, InStr(1, Test_Answer, ",")) <> "") Then
Msgbox "This is a REAL figure"
Else
Msgbox "This is an INTEGER"
End If
But what I was looking for was if VB had any function that could tell me this directly.
I think I will manage with my soultion.
Thanks !
By the way, HAPPY NEW YEAR !
Vestigo

010204, 14:51 #7Cavalier King Charles
 Join Date
 Dec 2002
 Location
 Préverenges, Switzerland
 Posts
 3,740
you have slightly changed the rules of the game: your question is more "checking a string from a textbox for decimal separator" than "checking for real or integer".
from my examples "12.00" etc you can see that your check is not perfect: i use "." and you use "," as decimal separator ...your check for "," will not work for me. additionally, users (ok, my users!) are fully capable of typing "12R45" as an integer and i guess you want to trap this too!
if you want to complicate your life a little, your check should examine each char in the textbox string against "0123456789" and reject as noninteger anything that doesn't match. along the lines of:
public function amIanInteger(strToCheck as string) as boolean
'returns FALSE if any char in strToCheck is not in "0123456789" else TRUE
dim intLoupe as integer
for intLoupe = 1 to len(strToCheck)
if instr(1, "0123456789", mid$(strToCheck, intLoupe, 1)) = 0 then
amIanInteger = FALSE
exit function
endif
next
amIanInteger = TRUE
exit function
(and maybe there is a way to use input masks to do what you want right on the form  i confess myself completely ignorant about input masks)
izycurrently using SS 2008R2

010204, 15:03 #8Grand Poobah
 Join Date
 Sep 2003
 Location
 MI
 Posts
 3,713
... And I agree with izy. Except for that is too much work ... Try something along the lines of this:
if isnumeric(MyTextBox.Value) then ' Have an int or real
if InStr(MyTextBox.Value,".") <> 0 then ' Have a real  substitute your decimal placeholder of choice ...
else
endif
endif

010204, 15:16 #9Cavalier King Charles
 Join Date
 Dec 2002
 Location
 Préverenges, Switzerland
 Posts
 3,740
M Owen,
i'll go with your version IF there is a programmatic way to determine the decimal separator in use on the system. otherwise i prefer my "international" version that handles any non number
...possibly adding a ltrim(rtrim()) refinement
izycurrently using SS 2008R2

010204, 15:24 #10Grand Poobah
 Join Date
 Sep 2003
 Location
 MI
 Posts
 3,713
izy,
If I remember correctly the decimal separator is specified by your locale ... That being so, I believe that the intrinsic functions have an awareness of this ... My being in good ol' Michigan, USA I won't be testing that theory anytime soon ...

010204, 17:34 #11Senior Member
 Join Date
 Nov 2002
 Location
 Norway
 Posts
 239
Nice engagement guys.
The number I like to evaluate is not a text string, it's a result of an mathematic expression. The result from my expression will never exceed 99, actually it will only very seldom exceed 9
I had to admit that I overlook the decimal separator case. :/
Therefore I tried your proposal "izyrider" (for intLoupe = 1 to len(strToCheck)....)
And it appears to do the job, so I go for that one. (Taken into consideration the length of the result, it will not be many loops before termination)
With the InStr(MyTextBox.Value,".") <> 0 proposal from "M Owen" I will end up with the same decimal separator problem as I ran into myself.
Thanks a lot!
sincerely
VestigoLast edited by vestigo; 010204 at 17:38.