代码如下:
double x = -1.0;
double y = 1.0;
final radians = angle * e / 180.0;
y = tan(radians) * x;
return Alignment(x, y);
然后当我打印出来时:
flutter: radians: -0.09060951997193932
flutter: y: 0.09085830765468121
flutter: x: -1.0
flutter: Alignment(-1.0, 0.1)
您可以看到 Alignment 对象中的 y 值 0.090858...被四舍五入为 0.1,这使我失去了应用程序所需的精度。
如何确保对齐对象不会对我的数字进行四舍五入?我想要的只是 y 值保持为 0.09085830765468121 而不是 0.1。
感谢任何帮助!
最佳答案
精度没有丢失。
当您在 dart 中打印对象时,会调用该对象的 toString 方法来获取该对象的字符串表示形式,然后将其打印到控制台。
Alignment类的toString
方法就是这个
...
return 'Alignment(${x.toStringAsFixed(1)}, '
'${y.toStringAsFixed(1)})';
和 toStringAsFixed 文档说
the result is the closest string representation with exactly [fractionDigits] digits after the decimal point. If [fractionDigits] equals 0 then the decimal point is omitted
如果你执行这个
const y = 0.09085830765468121;
const x = -1.0;
print(Alignment(x, y).y);
输出符合预期0.09085830765468121
。
关于flutter - 如何防止Flutter中的对齐对X和Y值进行四舍五入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61518732/