双打轮名单
我有一个许多双精度元素的列表,其中包含例如以下元素: [1.95, 1.986, 1.9602, 2.0477,...
我尝试使用此代码将元素四舍五入到两位小数:
List<double> listMax = List.from(data_snap()['animals']); //from Firebase
for (var value in listMax) {
value = roundDouble(value,2);
}
double roundDouble(double value, int places){
double mod = pow(10.0, places);
return ((value * mod).round().toDouble() / mod);
}
作为替代方案:
listMax.forEach((element) => roundDouble(element,2));
但这两个代码都没有成功。
I have a list of many double elements which contains for example the following elements: [1.95, 1.986, 1.9602, 2.0477,...
I tried to use this code to round the elements to two numbers of decimal places:
List<double> listMax = List.from(data_snap()['animals']); //from Firebase
for (var value in listMax) {
value = roundDouble(value,2);
}
double roundDouble(double value, int places){
double mod = pow(10.0, places);
return ((value * mod).round().toDouble() / mod);
}
And as an alternative:
listMax.forEach((element) => roundDouble(element,2));
But both codes are not successful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的代码中,您正在更改临时变量的值,要更改列表中的值,您的代码应如下所示:
in your codes you are changing the value of a temp variable, to change the value in the list your code should be like: