Android Question String to Number or add decimal to string

Jan Van Gastel

Member
Licensed User
All,

I have a code which generates strings with info from a telnet socket.
This part is working.

I have as string with a "number" in it, for example 201.
Now in real lieve this value is 20.1 , so I need to add a decimal to it.

2 options:

- Can I convert the string to a double, float... ?
- Can I add a decimal to the string ? 201 -> 20.1 ?
 

Mahares

Expert
Licensed User
Longtime User
Something like one of these two:
B4X:
Log(NumberFormat(201/10,1,1))    'displays: 20.1
Log($"${NumberFormat(201/10,1,1)}"$)   'displays: 20.1
 
Upvote 0

Jan Van Gastel

Member
Licensed User
Yes, but then I already need a double.
The "201" is in a string, so I need to convert the string to a number OR I need to put a decimal in between.
 
Upvote 0

nobbi59

Active Member
Licensed User
Longtime User
You could Looop through the string and check every char in the string if it is a number. if so you could add it to another string linke this:

dim number as string

For each c as Char in String
If Isnumber(c) then
number = number & c
end if
Next
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
in B4J this works fine

B4X:
Dim str1,str2 As String
str1="201"
str2=str1/10
Log(str2)

and output 20.1
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…