Android Question parseFloat equivalent in B4A

Discussion in 'Android Questions' started by Daniel-White, Apr 21, 2017.

  1. Daniel-White

    Daniel-White Active Member Licensed 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. ?
     
  2. eps

    eps Well-Known Member Licensed User

  3. klaus

    klaus Expert Licensed User

    What exactly is your problem?
    This works!
    Code:
    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.
     
  4. Cableguy

    Cableguy Expert Licensed User

    From what I could understand, he wants to be able to get rid of non-numerical characters in the string
     
    JordiCP likes this.
  5. JordiCP

    JordiCP Well-Known Member Licensed User

    Not an expert with Regex patterns, but this works ;)
    Code:
    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

    Code:
    '...
      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: Apr 21, 2017
    Daniel-White, DonManfred and Cableguy like this.
  6. Daniel-White

    Daniel-White Active Member Licensed User

  7. Daniel-White

    Daniel-White Active Member Licensed 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
     
    JordiCP likes this.
  8. JordiCP

    JordiCP Well-Known Member Licensed User

    did you try my code replacing the "52.12sd..." and put your version string instead?
     
    Daniel-White likes this.
  9. Daniel-White

    Daniel-White Active Member Licensed User

    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 :)
    Code:
    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: Apr 22, 2017
    Cableguy and JordiCP like this.
  10. JordiCP

    JordiCP Well-Known Member Licensed User

    LOL :D
     
    Daniel-White likes this.
Loading...