Uma*_*air 5 double decimal-point dart flutter
我已经创建了这个方法,该方法被调用来执行功能并进行计算,但对于一些超过 10 个小数点的计算,以双格式返回数字。如何使此代码仅显示最多 2 或 3 个小数点?
getProductPrice(int productID) {
var cart = cartlist.firstWhere((cart) => cart.product.id == productID,
orElse: () => null);
if (cart != null) {
var price= (cart.count)*(cart.product.price);
return price;
notifyListeners();
} else {
return 0.0;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 String toStringAsFixed(int fractionDigits)
返回此的小数点字符串表示形式。
this在计算字符串表示之前转换为双精度值。
如果 this 的绝对值大于或等于 10^21,则此方法返回由 this.toStringAsExponential() 计算的指数表示。否则,结果是最接近的字符串表示,在小数点后精确为fractionDigits 位。如果fractionDigits 等于0,则省略小数点。
参数fractionDigits 必须是满足以下条件的整数:0 <=fractionDigits <= 20。
例子:
1.toStringAsFixed(3); // 1.000
(4321.12345678).toStringAsFixed(3); // 4321.123
(4321.12345678).toStringAsFixed(5); // 4321.12346
123456789012345678901.toStringAsFixed(3); // 123456789012345683968.000
1000000000000000000000.toStringAsFixed(3); // 1e+21
5.25.toStringAsFixed(0); // 5
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看官方文档。
| 归档时间: |
|
| 查看次数: |
4560 次 |
| 最近记录: |