我找不到一种方法将双精度舍入到小数点后两位并删除 Dart 中的尾随零。我找到了一种舍入方法,但如果我尝试截断尾随零,它就不起作用。
这是我正在尝试做的事情:
double x = 5.0;
double y = 9.25843223423423;
double z = 10.10;
print(x); //Expected output --> 5
print(y); //Expected output --> 9.26
print(z); //Expected output --> 10.1
Run Code Online (Sandbox Code Playgroud)
编辑:
我找到了解决上面前两个打印语句的方法。我想我应该为搜索它的人添加它。
String getFormattedNumber( num ) {
var result;
if(num % 1 == 0) {
result = num.toInt();
} else {
result = num.toStringAsFixed(2);
}
return result.toString();
Run Code Online (Sandbox Code Playgroud)
}