Problems converting numbers from strings

Stulish

Active Member
Licensed User
Longtime User
Hi,

My app transfers data via serial communications, the data is in a string format.

my app seems to run ok but at times it will sometimes throw up and invalid double error, this normally happens if some of the sentances have not been sent and the the fields are blank.


My understanding was if i have a string that contains "00.03" in a variable SomeString

and then i create a variable SomeDouble by:

B4X:
Dim SomeDouble as double

when i use the following it should convert the string to a double automatically:

B4X:
SomeDouble = SomeString

is this thinking correct?

what is the outcome if the string contains nothing?

i have tried to catch the error using an if statement to say

B4X:
if SomeString<>"" then SomeDouble=SomeString
basically if the string contains nothing then do not do the conversion, but i still get the error


nothing seems to work
 

mc73

Well-Known Member
Licensed User
Longtime User
The returning string seems to be not an empty string, but the "". Anyway. perhaps trying this will solve the problem, though I don't recommend using it, but find the real cause:

B4X:
try
     SomeDouble=SomeString
catch
     SomeDouble=0
end try
 
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
thanks, i have managed to sort it out i used the ISNumber() command to see if the string could be converted to a number first.

no errors so far
 
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
Thanks Klause
 
Upvote 0

shaxboz

Member
Licensed User
Longtime User
Hi! I have problem with cinverting string to int.
I have two variables
I as int
S as string
s="150"
when I write I=s some devices showing error message "numberformat exception"
How can I convert string to int?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
>when I write I=s some devices showing error message "numberformat exception"

I dont see any code which is using numberformat!?

Post more relevant code and describe it more clearly. It´s hard to help with getting incomplete informations...

Maybe export your project as zip and upload it here... We then can have a look at it
 
Last edited:
Upvote 0

shaxboz

Member
Licensed User
Longtime User
ok
Dim ochkolari As String
Dim myochko As Int
Dim TextReader1 As TextReader
TextReader1.Initialize(File.OpenInput(File.DirInternal, "css.txt"))
ochkolari = TextReader1.ReadLine
TextReader1.Close
myochko=ochkolari
 
Upvote 0

eps

Expert
Licensed User
Longtime User
not S as string, it is like s as string

Please post the code that you are having trouble with, what you have posted is incorrect.

and use [ code ] [ /code] tags as well, otherwise it makes it difficult to read and therefore difficult to help.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Did the read work? You need to either toast message show that variable or log it or at least check it, don't assume that it's worked..
 
Upvote 0

shaxboz

Member
Licensed User
Longtime User
yes ofcourse, there will be problem with my file.
Thanks
But I have other question.
Now I am writing my config files to internal.
I am new, so I don`t know which is good write to internal or external storage?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…