Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


The trouble with Var's

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,65615
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> The trouble with Var's
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 The trouble with Var's - 10/29/2008 1:09:55 AM   
  Bongman

 

Posts: 7
Score: 0
Joined: 10/18/2008
Status: offline
Hi all

Im trying to write a script, that checks if a variable has decimal points or not.
I then want to include an if statement on this check.

However, whenever I perform arithmatic commands on these integers, they change to "Double" not Decimal as expected. Then, even if the var is changed so it has no decimal points (Ie a round number) it still remains a "double" not an "Integer" as expected.

here is the (quite unfinished) code:



      

Some help on this would be great :)

Cheers


Bongman
 
 
Post #: 1
 
 RE: The trouble with Var's - 10/29/2008 1:26:22 AM   
  ebgreen


Posts: 5251
Score: 31
Joined: 7/12/2005
Status: offline
This is one of the criticisms of dynamically typed languages. You rely on the interpreter to make typing decisions. If you don't like the typing decisions that it is making, use explicit type casting (CDbl, CInt, etc).

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Bongman)
 
 
Post #: 2
 
 RE: The trouble with Var's - 10/29/2008 1:40:12 AM   
  ebgreen


Posts: 5251
Score: 31
Joined: 7/12/2005
Status: offline
In your code, the variables (r1,r2,r3,r4) will never have a typename of integer since they will always have a decimal component.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: The trouble with Var's - 10/29/2008 1:49:43 AM   
  Bongman

 

Posts: 7
Score: 0
Joined: 10/18/2008
Status: offline
Hi Ebgreen

typename(r1) returns "double" for r1,r2,r3 &,r4 which is a "64-bit floating point" number.

I expected either Integer or Decimal.

Can you think of any other way of doing this? Prehaps converting the Double back to integer, or prehaps "long"?

All I want to do is determine if the result is a whole number or not.

Regards

Bongman

(in reply to ebgreen)
 
 
Post #: 4
 
 RE: The trouble with Var's - 10/29/2008 1:54:32 AM   
  ebgreen


Posts: 5251
Score: 31
Joined: 7/12/2005
Status: offline
What do you mean by result? The result variable is always a whole number in your code.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Bongman)
 
 
Post #: 5
 
 RE: The trouble with Var's - 10/29/2008 2:14:31 AM   
  Bongman

 

Posts: 7
Score: 0
Joined: 10/18/2008
Status: offline
Sorry, I shouldn't have referred to it as "result"

I meant r1,r2,r3 & r4.

I want to record (echo or add a line in a text file)  "result" when r1,r2,r3 & r4 all return a whole number.

Regards

Bongman

(in reply to ebgreen)
 
 
Post #: 6
 
 RE: The trouble with Var's - 10/29/2008 2:25:07 AM   
  ebgreen


Posts: 5251
Score: 31
Joined: 7/12/2005
Status: offline
Try this, but be forewarned that Ints can't be very large.


      

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Bongman)
 
 
Post #: 7
 
 RE: The trouble with Var's - 10/29/2008 2:29:12 AM   
  ehvbs

 

Posts: 2224
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
If CInt() - conversion to Integer - fails because of the data range or problems with rounding,
use Fix() - truncation of fractional part.

(in reply to ebgreen)
 
 
Post #: 8
 
 RE: The trouble with Var's - 10/29/2008 2:41:17 AM   
  ebgreen


Posts: 5251
Score: 31
Joined: 7/12/2005
Status: offline
Good advice.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ehvbs)
 
 
Post #: 9
 
 RE: The trouble with Var's - 10/29/2008 12:51:33 PM   
  TNO


Posts: 1402
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
why not convert to a string and check for the existence of the decimal point?

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: The trouble with Var's - 10/29/2008 5:58:10 PM   
  ehvbs

 

Posts: 2224
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Because being a whole number or not is a mathematical not a philological question.

(in reply to TNO)
 
 
Post #: 11
 
 RE: The trouble with Var's - 10/29/2008 10:56:19 PM   
  TNO


Posts: 1402
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
In this case its not even a mathematical one, its whether the decimal exists or not, ignoring whole numbers.

So..., why not this and use the type coercion to our advantage?


      

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ehvbs)
 
 
Post #: 12
 
 RE: The trouble with Var's - 10/30/2008 12:43:59 AM   
  ebgreen


Posts: 5251
Score: 31
Joined: 7/12/2005
Status: offline
I debated that as a way to do the IsWholeNumber function. At the time I couldn't remember how something like this would be represented as a string:

nNum = 3
nDiv = 100
nFac = 1000

dblTemp = nNum/nDiv
WScript.Echo CStr(dblTemp*1000)


I couldn't remember if that would represent the string as 30 or 30.00. I did some testing and it does 30. SO looking for the decimal should work just fine.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to TNO)
 
 
Post #: 13
 
 RE: The trouble with Var's - 10/30/2008 2:00:43 AM   
  ehvbs

 

Posts: 2224
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Don't rely on CStr() or other formatting/conversion:


      

output (DE locale):


      

Math alone is evil enough, don't give Philology a chance to make things worse.

(in reply to ebgreen)
 
 
Post #: 14
 
 RE: The trouble with Var's - 10/30/2008 3:56:00 AM   
  TNO


Posts: 1402
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
The OP's question was generic and simple and a generic and simple solution was provided. Nothing was asked about international formatting, nor issues regarding E notation, both of which are generally rare situations. At any rate he did not ask if it was a whole number, he asked if it contained a decimal point or not. If E notation is given, then an E notation number does not contain a decimal which is "correct", the same goes with the international number format. He didn't ask for the separator, he literally asked for the decimal point. Of course I could also ask if
3.00 should pass or fail this test (if precision has to be maintained), but regardless these issues were not brought up so a solution was not given. I didn't assume what he meant I offered a solution to the literal question

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ehvbs)
 
 
Post #: 15
 
 RE: The trouble with Var's - 10/30/2008 4:55:13 AM   
  ehvbs

 

Posts: 2224
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi TNO,

having problems to understand your contribution, I'd like to ask: Do you propose to
use "number to string conversion" and "examinating the result for a certain character"
to determine whether the number has/contains a fractional part or not?

Regards

ehvbs

(in reply to TNO)
 
 
Post #: 16
 
 RE: The trouble with Var's - 10/30/2008 5:10:15 AM   
  TNO


Posts: 1402
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
With the current information given by the OP, yes that is the proposal I offer since it solves the problem given with the least amount of code. I was not seeking a grand solution for what seems to me to be a  domain specific problem

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ehvbs)
 
 
Post #: 17
 
 RE: The trouble with Var's - 10/30/2008 5:25:59 AM   
  ehvbs

 

Posts: 2224
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
(1) How would you the define the domain for which your IsWholeNumber( <SomeNumber> ) function will
     give correct answers?

(2) Please show such a function which has less code than
       Function IsWholeNumM( nNum )
        IsWholeNumM = nNum = Fix( nNum )
       End Function

(in reply to TNO)
 
 
Post #: 18
 
 RE: The trouble with Var's - 10/30/2008 5:50:50 AM   
  TNO


Posts: 1402
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
You're giving me a loaded question. As I just said I was not solving the problem of whole number vs not whole number, I was solving the existence of the decimal point or not, which provides the same solution to the OP's problem given.

So in the context of that,

#1 - The OP's examples define the domain
#2 -  (based on the premise I just gave): InStr(CStr(Foo),".") = 0

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ehvbs)
 
 
Post #: 19
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> The trouble with Var's Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts