Android Question Convert string to double returns zero

BitsAndBytes

Active Member
Licensed User
I have a problem i want to convert this string "999,999.00" to double variable but this function returns me zero

B4X:
dim doubleValue as double
textViewValue = "999,999.00"
doubleValue = returnDouble (textViewValue)

Sub returnDouble(strValue As String) As Double
   If IsNumber(strVal) Then
       Dim doubleValue As Double : doubleValue = strVal
       Return doubleValue
   Else
       Return 0
   End If
End Sub
 

mangojack

Well-Known Member
Licensed User
Longtime User
B4X:
Sub returnDouble(strValue As String) As Double
If IsNumber(strVal) Then ' @@ strValue   vs  strVal
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
In addition to @mangojack tip, you need to remove the comma from the following line:
textViewValue = "999,999.00"
or use the following:
B4X:
Sub returnDouble(strValue As String) As Double
    If IsNumber(strValue.Replace(",","")) Then
        Dim doubleValue As Double : doubleValue = strValue
        Return doubleValue
    Else
        Return 0
    End If
End Sub


EDIT: the following looks better:
B4X:
Sub returnDouble(strValue As String) As Double
    strValue=strValue.Replace(",","")
    If IsNumber(strValue) Then
        Dim doubleValue As Double : doubleValue = strValue
        Return doubleValue
    Else
        Return 0
    End If
End Sub
 
Last edited:
Upvote 0
Top