Android Question parseFloat equivalent in B4A

Daniel-White

Active Member
Licensed User
Longtime User
Hi, Folks.

I have this in javascript parseFloat, The parseFloat() function parses a string and returns a floating point number. How to do the same in B4A? an example.

Javascript
version = parseFloat(5.12a);
console.log(version);

The log will show "5.12" without "a" how can I do it in B4A, the problem sometimes the version can be 5.1b 5.11b I would like to obtain only the numbers without the letter. ?
 

klaus

Expert
Licensed User
Longtime User
What exactly is your problem?
This works!
B4X:
Private a As Float
a = "5.12"
Log(a)
B4A converts automatically.
Is there a reason why you use Floats insted of Doubles?
If I remember well, in B4A, all calculations are done in Double and converted back to the variable type.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
What exactly is your problem?
This works!
B4X:
Private a As Float
a = "5.12"
Log(a)
B4A converts automatically.

From what I could understand, he wants to be able to get rid of non-numerical characters in the string
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Not an expert with Regex patterns, but this works ;)
B4X:
  Dim Matcher1 As Matcher = Regex.Matcher("\d+\.\d+","54.12sd8911z")
   Dim version As Float
   If Matcher1.Find Then
     version = Matcher1.Match
   End If
   Log(version)

--Edit--

if the version string does not contain the "." (for instance "5c"), this will fail. Then you can look for a second pattern when the "if Matcher.Find" condition is false

B4X:
  '...
  Else
     Dim Matcher1 As Matcher = Regex.Matcher("\d+","54.12sd8911z")
     '... 
  End if
[CODE]

or build a more complex one, but don't know how.
 
Last edited:
Upvote 0

Daniel-White

Active Member
Licensed User
Longtime User
Thank guys, for try to help. The parseFloat() in javascript, apparently is little bit powerful,:eek: because it does not matter if version = "5.12a" or version ="assdjf5.12somethinhere" It always return 5.12 take a look and run an example here https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_parsefloat

Perhaps I can split and kick out the "a" but sometimes the version can be version = "5.1b" or version = "5.67c" (one or two digits after the ".") the nice, the version always have a tail a letter "a" "b" and so on and not more of 3 digits after "." I am thinking, to do a routine to detect how long is version after the "." is size = 2 ok kick the letter in position 3, if size =1 kick the letter in position 2.

Perhaps you can find a clever way to do this o_O
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Thank guys, for try to help. The parseFloat() in javascript, apparently is little bit powerful,:eek: because it does not matter if version = "5.12a" or version ="assdjf5.12somethinhere" It always return 5.12 take a look and run an example here https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_parsefloat

Perhaps I can split and kick out the "a" but sometimes the version can be version = "5.1b" or version = "5.67c" (one or two digits after the ".") the nice, the version always have a tail a letter "a" "b" and so on and not more of 3 digits after "." I am thinking, to do a routine to detect how long is version after the "." is size = 2 ok kick the letter in position 3, if size =1 kick the letter in position 2.

Perhaps you can find a clever way to do this o_O
did you try my code replacing the "52.12sd..." and put your version string instead?
 
Upvote 0

Daniel-White

Active Member
Licensed User
Longtime User
did you try my code replacing the "52.12sd..." and put your version string instead?
Oooooo Yes I tried it, but in the wrong way what a shame LoL :confused: you are 1000% right. I only change the Dim version As Float by Dim version As Double.

And return the perfect value does not matter if 5.12a or 5.1a etc, perfect. so we have now in B4A this Sub parseFloatJordiCPstyle :)
B4X:
Sub Activity_Create(FirstTime As Boolean)
    parseFloatJordiCPstyle("5.1a")
    parseFloatJordiCPstyle("5.52a")
End Sub


Sub parseFloatJordiCPstyle (VersionString As String)  ' :)
Dim Matcher1 As Matcher = Regex.Matcher("\d+\.\d+",VersionString)
   Dim Version As Double
   If Matcher1.Find Then
     Version = Matcher1.Match
   End If
   Log(Version)
End Sub

return:
5.1
5.52

:D Much obliged
 
Last edited:
Upvote 0
Top