0

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! I would never have found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.