简体   繁体   中英

How to make sum of double value in a string in flutter?

Hello I have a simple string formatted like that (2.0, 1.0, 1.0, 3.0, 5.0)

I don't found how to simply make a sum of each value 2.0+1.0+1.0+3.0+5.0 = 11.0

Thank you

One way you can do is like this.

  String str = "(2.0, 1.0, 1.0, 3.0, 5.0)";
  str = str.replaceAll("(","");
  str = str.replaceAll(")","");
  List<String> strDoubles = str.split(", ");
  
  double sum = 0;
  strDoubles.forEach((String item){
    sum = sum + double.parse(item);
  });
  print(sum);       //<-- prints 12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM