PI 十进制使用 Math.Round

PI decimal using Math.Round

我正在寻找以下问题的答案:

期望的行为

将数学常数 pi 舍入到 2 位或 4 位小数。

当前代码

我试过以下方法:

Double piRounded;
piRounded = Math.Round.PI(4);
piRounded = Math.Round.PI(2);

当前结果

这会导致以下错误:

'System.Math.Round(double)' is a 'method', which is not valid in the given context

Math.Round 在您的代码中被指定为 属性,而不是方法调用。

Math.Round(...) 一样将参数放在圆函数中 - 您需要传入 PI 常量。

Math.Round(Math.PI, 4)(如评论中所述)。

使用:

Math.Round(Math.PI, 2);

Math.Round(Math.PI, 4);

分别