简体   繁体   English

如何在颤振中对字符串中的双值求和?

[英]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)你好,我有一个简单的字符串格式(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我没有找到如何简单地对每个值求和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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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