Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Convert a String to a Single
Goto page Previous  1, 2
 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
twesthoff



Joined: 17 Mar 2006
Posts: 199
Location: Fredericksburg, VA

Posted: 19 June 2007, 23:49 PM    Post subject: Convert a String to a Single Reply with quote

No, it returns whatever type numeric variable it is assigned to.

This VB6 code when run,

Private Sub Command1_Click()
Dim x As Single
Dim y As Double
Dim z As Integer
x = Val("3.1415929")
Debug.Print "X="; x
y = Val("3.1415929")
Debug.Print "Y="; y
z = Val("3.1415929")
Debug.Print "Z="; z
End Sub


Gives:
X= 3.141593
Y= 3.1415929
Z= 3



-----Original Message-----
From: ZBasic [mailto:zbasic.forum@zbasic.net]
Sent: Tue 6/19/2007 5:51 PM
To: zbasic.forum@zbasic.net
Cc:
Subject: RE: Convert a String to a Single

Yes but Val() returns a Double not a Single..
Back to top
mdown



Joined: 03 Feb 2006
Posts: 62
Location: Dallas, Texas

Posted: 19 June 2007, 23:52 PM    Post subject: Reply with quote

dlh wrote:
In VB4 I think it returned Integer, Long or Double depending on the content of the string. Or maybe it was just that VB's type conversion made it seem that way. CDbl was recommended for doubles because Val only recognized a period as a decimal separator while CDbl recognized other separators.


Yea VB is pretty relaxed about variable types.. its a bad thing... VB.Net is lots more strict.
Back to top
mdown



Joined: 03 Feb 2006
Posts: 62
Location: Dallas, Texas

Posted: 20 June 2007, 0:04 AM    Post subject: Re: Convert a String to a Single Reply with quote

twesthoff wrote:
No, it returns whatever type numeric variable it is assigned to.

This VB6 code when run,

Private Sub Command1_Click()
Dim x As Single
Dim y As Double
Dim z As Integer
x = Val("3.1415929")
Debug.Print "X="; x
y = Val("3.1415929")
Debug.Print "Y="; y
z = Val("3.1415929")
Debug.Print "Z="; z
End Sub


Gives:
X= 3.141593
Y= 3.1415929
Z= 3



-----Original Message-----
From: ZBasic [mailto:zbasic.forum@zbasic.net]
Sent: Tue 6/19/2007 5:51 PM
To: zbasic.forum@zbasic.net
Cc:
Subject: RE: Convert a String to a Single

Yes but Val() returns a Double not a Single..


http://msdn2.microsoft.com/en-us/library/aa263400(VS.60).aspx

says "Returns the numbers contained in a string as a numeric value of appropriate type." whatever that means...

However if you type "a = val(" the quickhelp pops up "Val(String as String) as Double"

so its anyones guess, must be a dinosaur left over from yester-year.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2593
Location: Portland, OR

Posted: 20 June 2007, 0:15 AM    Post subject: Re: Convert a String to a Single Reply with quote

twesthoff wrote:
No, it returns whatever type numeric variable it is assigned to.
I suspect that it only appears that way due to the automatic type conversion in VB6. Consider the more general case, where Val() is used in an expression or as a parameter to a subroutine or function that may take different types. For example,
Code:
Debug.Print CStr(Val(s))
If 's' is "2.6" this prints 2.6 but if s is "2" it prints 2.
Back to top
twesthoff



Joined: 17 Mar 2006
Posts: 199
Location: Fredericksburg, VA

Posted: 20 June 2007, 1:23 AM    Post subject: Convert a String to a Single Reply with quote

Don,
I would expect it to do exactly that. However in VB you would not need the Cstr(), and would just use: Debug.print val(s)

I'm not certain what converting a string to a numeric with val()and then back to a string with Cstr() shows us.
In the "old" days there was not Cstr(), only Val() and Str$() (or more recently Str() ) which only worked with singles as there were not doubles available at that time. I think as Dave said, that Cstr() was added because Val() was written only to use the "." and not other international separation characters.
Val() has another quirk in that it will take a string like "123 abc" and return a value of 123. This was not intentional, but the original function was written poorly and just worked that way. Then it became a "feature" later on...

Val() has been part of almost every version Basic I have used since Basic first appeared, except for some of the "Tiny" basics that were written for some of the early microprocessors. All I know is that it always converted a string representation of a number into a numeric value.
Tom

-----Original Message-----
From: ZBasic [mailto:zbasic.forum@zbasic.net]
Sent: Tue 6/19/2007 8:15 PM
To: zbasic.forum@zbasic.net
Cc:
Subject: RE: Convert a String to a Single


twesthoff wrote:
Quote:
No, it returns whatever type numeric variable it is assigned to.
I suspect that it only appears that way due to the automatic type conversion in VB6. Consider the more general case, where Val() is used in an expression or as a parameter to a subroutine or function that may take different types. For example,
Debug.Print CStr(Val(s))If 's' is "2.6" this prints 2.6 but if s is "2" it prints 2.

------------------------
- Don Kinzer
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2593
Location: Portland, OR

Posted: 20 June 2007, 1:41 AM    Post subject: Re: Convert a String to a Single Reply with quote

twesthoff wrote:
I'm not certain what converting a string to a numeric with val()and then back to a string with Cstr() shows us.
CStr() is a polymorphic function, i.e., it accepts parameters of various types. Since any type is acceptable to CStr() there should be no need for an automatic type conversion and therefore one might "see" the actual type returned by Val().

That said, the test is inconclusive because CStr(f) when f is a Single with the value 2.0 also prints 2 with no decimal point.

As a further attempt to determine the return type of Val(), consider the following VB6 program:
Code:
Dim s1 As String
Dim s2 As String

Sub Main()
    s1 = "1.23"
    s2 = &H123
    Debug.Print TypeName(Val(s1))
    Debug.Print TypeName(Val(s2))
End Sub

When executed, this displays
Code:
Double
Double
suggesting that Val() does, in fact, return a type Double.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Goto page Previous  1, 2
Page 2 of 2

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group