Nitneuq
Nitneuq

Reputation: 5042

How to convert to double

I validate the fonction of extract gps latitude and longitude with regex, but currently it's a String , and map_view accept only double

previous problem : how to display a regex result in flutter

I tried to use this to convert in double but it doesn't work

    RegExp regExp = new RegExp(            //Here is the regex fonction to extract long, lat
       r"maps:google\.com\/maps\?q=(-?[0-9]+.[0-9]+),(-?[0-9]+.[0-9]+)",
      );
      var match = regExp.firstMatch(input);
      group1 = match.group(1);   //match groupe seems to be an int
      group2 = match.group(2);

      var long2 = double.parse('$group1');
      assert(long2 is double);
      var lat2 = double.parse('$group2');
      assert(lat2 is double);

Upvotes: 57

Views: 156775

Answers (3)

Axel Asa
Axel Asa

Reputation: 43

You should use the method below. double.parse(inset your string here), and if you want to switch anthing to an int you should use the method below. int.parse(enter your value here)

Upvotes: 0

user2685314
user2685314

Reputation: 1069

Why not use just group1 as in double.parse(group1)

Upvotes: 6

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657967

Remove the quotes from

 var long2 = double.parse('$group1');

to

 var long2 = double.parse($group1);

You can also use

var long2 = double.tryParse($group1);

or to also accept numbers without fractions

var long2 = num.tryParse($group1)?.toDouble();

to not get an exception when the string in $group1 can not be converted to a valid double.

Upvotes: 115

Related Questions