How to convert a string to integer

gkumar

Active Member
Licensed User
Longtime User
How to convert a string to integer, For example If i have,
Dim strValue as string
strValue = "100"
 

stefanobusetto

Active Member
Licensed User
Longtime User
if you want to trap conversion errors

Try
valString = strValue
Catch
MsgBox ( "the string is not a valid integer" , "error" )
End Try
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
These will be nice for Margret's collection.

Rolf
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If you know the content of a string is a number, there is no need to convert it to an integer or float if you are going to use it in calculations.
Am I missing something.? See below code:

B4X:
Dim strValue As String
Dim Result As Float
strValue = "100"
Result=Sqrt(strValue)
Msgbox("The square root of " & strValue & " is " &  Result,"")
 
Upvote 0

RichardBernard

Member
Licensed User
Longtime User
If you know the content of a string is a number, there is no need to convert it to an integer or float if you are going to use it in calculations.
Am I missing something.? See below code:

B4X:
Dim strValue As String
Dim Result As Float
strValue = "100"
Result=Sqrt(strValue)
Msgbox("The square root of " & strValue & " is " &  Result,"")
Hi,

I'm using:
B4X:
   Dim Waiting_Time As EditText
   Dim Waiting_Time_Int As Int
   Dim tmpString As String
----------------------------
tmpString = Waiting_Time.Text
Waiting_Time_Int = tmpString + 1
tmpString = Waiting_Time_Int
Waiting_Time.Text = tmpString

I tried many different ways of implementing the above code but I keep on getting the error:
B4X:
java.lang.NumberFormatException: Invalid double: ""
Installing file.
PackageAdded: package:ce3.arsham.CE3RVCtrl
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_tmr1_tick (B4A line: 195)
Waiting_Time_Int = tmpString + 1
java.lang.NumberFormatException: Invalid double: ""
   at java.lang.StringToReal.invalidReal(StringToReal.java:63)
   at java.lang.StringToReal.parseDouble(StringToReal.java:248)
   at java.lang.Double.parseDouble(Double.java:295)
   at ce3.arsham.CE3RVCtrl.main._tmr1_tick(main.java:789)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5039)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   at dalvik.system.NativeStart.main(Native Method)

Can anyone please help me find out what's going on?

Thanks,
R
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You should just be able to do

B4X:
Waiting_Time.Text=Waiting_Time.Text+1

If Wating_Time.Text is a number. Try:

B4X:
If IsNumber(Waiting_Time.Text) then
    Waiting_Time.Text=Waiting_Time.Text+1
Else
    Log(Waiting_Time.Text &" is not a number")
End If
 
Last edited:
Upvote 0

RichardBernard

Member
Licensed User
Longtime User
Thanks for the hint Steve! I found the problem was the object was not initialized.

One more question, how do I get rid of the decimal that is added after the assignment (i.e "0"+1="1.0") ?

R
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have a look at numberformat and numberformat2.

-------------------
Sent via Tapatalk
 
Upvote 0
Top