Best way to convert a number to a string

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Is there a simple replacement in B4A for VB's "Val" command; e.g.: i = Val(editText1) ?

I've spent a lot of time experimenting and searching for an answer and can't find it.
 
Last edited:

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Basic4android automatically converts strings to numbers as needed:
B4X:
Dim i As Int
i = EditText.Text + 3
You can also use IsNumber to check if the string can be converted to number.

I tried that and got an error.

Edit: I just tried it again using

If IsNumber(EditText) Then i = EditText Else i = 0

and didn't get the error. I didn't realize that IsNumber was a requirement.
 
Last edited:
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Maybe this will work

If IsNumber(EditText.text) Then i = EditText.text Else i = 0

We crossed posts. I put that in an edit to my last post 10 minutes ago. I was hoping I did it before anyone had time to reply. You're just too fast!

The documentation does not read like IsNumber is a requirement, but it appears to be. I'll have to go to the Wish List forum and put in a vote for Val(string).

It also seems that when an EditText view is set to NUMBERS for Input Type, the value returned should be a number instead of a string, but maybe there is some practical reason for returning a string that I'm not considering.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
EditText.Text is always a string. The compiler automatically adds the code required to parse the string as a number when needed.
If the EditText.Text is empty or contains any string value that cannot be parsed as a number it will throw an error. IsNumber is a safety check to make sure that the value can be parsed correctly.
 
Upvote 0
Top