VAL keyword

SarahWard

Banned
I would love a VAL keyword.

f = val(s)

Where f is floating point number and s is string.

Any numerical value found within the string is copied into float b.

(If no numerical value is found within string s then then f = 0)
 

SarahWard

Banned
Thank you guys. :)

But this code, and the isnumber keyword do not do what the old basic 'VAL' did.

I need a keyword that can parse a string and return the numeric value even if that value is part of an alphabetic string.

Take string: "COM4". Using the above code you will not get '4' as the numeric part, you will get a 'false' value (0).

I am sure the old programming keyword VAL ("COM4") would return the numeric value of '4'.

Yes, I could write a routine that parsed the string but given that the VAL keyword has always been part of basic since the early DOS days I would prefer it be part of the math subset of keywords in this version of basic. :) Very useful function and i miss it (although I don't miss MSDOS. :)

aGraham, your routine (below) would return a '0' if the string 'str' equaled: "COM4", whereas VAL(str) would return a '4'.

ub Val("COM4")
If IsNumber(str) Then
Return str
Else
Return 0 ...... RETURNS WITH '0' when it should return with '4'
End If
End Sub

FWIW: Your extra libraries are just excellent, Andrew. They have really enhanced b4ppc for me. Thank you so much. :)
 
Last edited:

dlfallen

Active Member
Licensed User
Longtime User
Actually, that is not how the VB Val function works. To quote the VB help file: "The Val function stops reading the string at the first character it can't recognize as part of a number."

I tried it, and Val("Com4") returns 0. Val("4Com") would return 4.

I got the same results in the old DOS QuickBasic, and I'm pretty sure the Val function worked this way in all flavors of DOS basic.
 

mjcoon

Well-Known Member
Licensed User
Returning zero as a fault condition is not very helpful, since it is a good numeric value. Is there not a "not a number" value instead? Or I think I would vote for an error condition (if given the chance!).
 

agraham

Expert
Licensed User
Longtime User
Returning zero as a fault condition is not very helpful
It's not necessarily a fault depending upon the use required of the function. I've just run my 26 year old copy of GW-Basic and in that Val behaves as dfallen indicated as it also does in my copy of QuickBasic 4.5.
 

SarahWard

Banned
It was the VAL from QuickBasic 4.5 that I remembered. Also val from early visual basic.
DlFallen: I guess it didn't do exactly what I had presumed.

I agree with aGraham. Zero as a 'false' flag is good because we used to use zero as a 'false' flag before the age of VB.

I have needed the old VAL keyword several times recently in my various freeware programs.


I think, in practice, the numeric value was usually the only characters in the string. Often it was the string and VAL would simply convert it to a usable numeric value. I suppose IsNumber would do this as long as the string was pure numeric value.
 

dlfallen

Active Member
Licensed User
Longtime User
VAL had other "features" as well. For one, it would strip out spaces, linefeeds, and tabs. So, for example, VAL("12 34") woud return 1234.

One good thing is that it would recognize &H and &O as hex or octal numbers.

The bad news is that it only recognized the the period as the decimal separator so it was not useful in international applications.

As I recall, I had a custom VAL function that I used in my aplications. Not hard to code and it did what I wanted the way I wanted it done. When speed was important, I would code it in assembler. Of course, that was in the pre-Windows era and processors were slow. I have no inclination to program in assembler under Windows.
 

SarahWard

Banned
VAL had other "features" as well. For one, it would strip out spaces, linefeeds, and tabs. So, for example, VAL("12 34") woud return 1234.

One good thing is that it would recognize &H and &O as hex or octal numbers.

The bad news is that it only recognized the the period as the decimal separator so it was not useful in international applications.

As I recall, I had a custom VAL function that I used in my aplications. Not hard to code and it did what I wanted the way I wanted it done. When speed was important, I would code it in assembler. Of course, that was in the pre-Windows era and processors were slow. I have no inclination to program in assembler under Windows.

It still sounds like a really useful keyword. :)

Assembler under windows would be like building a coastal barrier my moving one pebble at a time.:)
 

taximania

Well-Known Member
Licensed User
Longtime User
Here's a starter for you. If you haven't done it already.

B4X:
Sub VAL(s)
temp = ""
temp2=0
flag = 0
If StrLength(s) = 0 Then
   Return 0
Else
   len = StrLength(s)
   For a = 0 To len -1
      If IsNumber(StrAt(s,a)) Then
         temp = temp & StrAt(s,a)
      Else
         If StrAt(s,a) = "." Then
            If flag = 0 Then
               flag = 1
               temp = temp & "."
            End If
         End If
      End If
   Next
   If temp = "" OR temp = "." Then
      Return 0
   Else
      temp2 = temp2 + temp
      Return temp2
   End If
End If
End Sub

Returns 0 for no number found.
The first '.' found is treat as a decimal point. All others are ignored.
Trims excessive leading and trailing '0's.
 

beacon

Member
Licensed User
Longtime User
I agree that it would be very useful.

Here is a typical use for Val():

A User types a time into a textbox, say "18:36".

I extract the hours portion using stringsex.mid to give a string called (say) MyHours holding "18".

Now I can code

if val(MyHours)<12 then
.......
end if

See how the computation is treating the string "18" as the value 18.

Voila!

I use this Val() function in VB6 a lot.

Is this helpful?
:sign0104:
 

derez

Expert
Licensed User
Longtime User
It can be done like this also without the Val sub:
if MyHours + 0 < 12 then... because the operation on the string converts it to a number.
 

mjcoon

Well-Known Member
Licensed User
As Basic4ppc is untyped, or more accurately does automatic type conversion, a numeric string is treated the same a numeric literal so Val is not needed in this case.

But there is a trap there that I fell into a few days ago. I was wishing to "correct" a "North" bearing value that is delivered by my GPS via NMEA as "360.0" which is not a valid value. (The GPS actually prevents me from entering that value via its user interface!)

So I wrote:
B4X:
If headingDirection = 360 Then headingDirection = 0

What was in my head was that Basic4PPC would convert to numeric for the comparison but it doesn't and finds that "360.0" is not equal to 360. I would have used a Val() if Basic4PPC had one.

Mike.
 

agraham

Expert
Licensed User
Longtime User
What was in my head was that Basic4PPC would convert to numeric for the comparison but it doesn't and finds that "360.0" is not equal to 360.
Yes, I'm afraid that the Basic4ppc rule for logical comparisons is not documented but it seems that all comparisons are made as strings and typed variables are converted to a string for the comparison.
I would have used a Val() if Basic4PPC had one.
Int or Abs would work in your situation.

As an aside there is an "interesting" difference between .NET and Java(Android) that I have encountered. In .NET a Double and an Integer containing the same integer value both convert to identical strings. e.g. "360". In Java the Double conversion always ends with a decimal place, even for an integer value so the resulting strings are not the same. e.g "360.0" for a Double and "360" for an Integer.
 

ceaser

Active Member
Licensed User
Hi

How abot having a little as follows:

'S = String to be checked for a number

N=""
For i=0 to StrLength(s)
i1=Asc(SubString(S,i,1))
if i1=46 or (i1>=48 and i1<=57) then
N=N & i1
end if
Next i
Msgbox(N)

I have not checked it, but along this line should work.

Regards
Michael
 

mjcoon

Well-Known Member
Licensed User
'S = String to be checked for a number ...

It would be (slightly!) interesting to know if the built-in automatic conversions can cope with all the number representations that can be generated by the Format() function... (Scientific notation; "." and "," separators; etc.)

Mike.
 
Top