Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert to double

Tags:

flutter

dart

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); 
like image 850
Nitneuq Avatar asked Jun 08 '18 10:06

Nitneuq


People also ask

How do you convert to double in Python?

Use float() method or decimal() method to convert string to double in Python. Conversion of string to double is the same as the conversion of string to float.


2 Answers

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.

like image 196
Günter Zöchbauer Avatar answered Sep 22 '22 13:09

Günter Zöchbauer


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

like image 38
user2685314 Avatar answered Sep 22 '22 13:09

user2685314