给定一个double,我想将它舍入到小数点后的给定数量的精度点,类似于PHP的round()函数.
我在Dart文档中找到的最接近的东西是double.toStringAsPrecision(),但这不是我需要的,因为它包括精度总点数小数点之前的数字.
例如,使用toStringAsPrecision(3):
0.123456789 rounds to 0.123
9.123456789 rounds to 9.12
98.123456789 rounds to 98.1
987.123456789 rounds to 987
9876.123456789 rounds to 9.88e+3
Run Code Online (Sandbox Code Playgroud)
随着数量的增加,我在小数位后相应地失去了精度.
以下产生以下错误:
int calc_ranks(ranks)
{
double multiplier = .5;
return multiplier * ranks;
}
Run Code Online (Sandbox Code Playgroud)
返回类型'double'不是'int',由方法'calc_ranks'定义
如何舍入/强制转换为int?
我没有办法在Dart中找到一个数字?
import 'dart:math';
main() {
print(Math.round(5.5)); // Error!
}
Run Code Online (Sandbox Code Playgroud)
Flutter 中如何将数字四舍五入到最接近的整数?
Numround方法四舍五入到最接近的整数。怎么总能四舍五入?