C# 中的舍入函数

发布于 2024-12-08 09:37:45 字数 373 浏览 2 评论 0原文

   public static decimal Round(
decimal d,
int decimals
    )

Decimals参数指定返回值中的小数位数,范围为0到28。如果decimals为零,则返回整数。

如果d中小数点右边第一位的值为5,则小数点位置上的数字如果是奇数则向上舍入,如果是偶数则保持不变。如果 d 的精度小于小数,则原样返回 d。

    Math.Round(3.44, 1); //Returns 3.4.
   Math.Round(3.45, 1); //Returns 3.4.

为什么 3.45 返回 3.4..我无法理解这个输出。任何人都可以帮忙吗

   public static decimal Round(
decimal d,
int decimals
    )

The decimals parameter specifies the number of fractional digits in the return value and ranges from 0 to 28. If decimals is zero, an integer is returned.

If the value of the first digit in d to the right of the decimals decimal position is 5, the digit in the decimals position is rounded up if it is odd, or left unchanged if it is even. If the precision of d is less than decimals, d is returned unchanged.

    Math.Round(3.44, 1); //Returns 3.4.
   Math.Round(3.45, 1); //Returns 3.4.

Why 3.45 is returning 3.4..I am unable to understand this output.Can anyone help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东京女 2024-12-15 09:37:45

您可以通过使用采用 MidpointRounding 参数的 Round 重载来更改此行为,来自 MSDN:

ToEven(默认,AKA 银行家舍入)当一个数字介于其他两个数字之间时,它会向最接近的偶数舍入。
AwayFromZero 当一个数字介于另外两个数字之间时,它会向远离零的最近数字舍入。

You can change this behaviour by using the Round overload which take the MidpointRounding parameter, from MSDN:

ToEven (default, AKA Bankers Rounding) When a number is halfway between two others, it is rounded toward the nearest even number.
AwayFromZero When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

椒妓 2024-12-15 09:37:45

就像你说的,

如果d中小数点右边第一位的值为5,则小数点位置上的数字如果是奇数则四舍五入,如果是偶数则保持不变。

对于 3.45,小数点右边第一位是 5,由于 4 是偶数,因此保持不变。这是一种非常标准的舍入方式,因为如果始终将 5 舍入,则平均值和总和等权重可能会高于应有的值。

Like you said,

If the value of the first digit in d to the right of the decimals decimal position is 5, the digit in the decimals position is rounded up if it is odd, or left unchanged if it is even.

With 3.45, the first digit to the right of the decimal is 5, and since 4 is even, it's left unchanged. This is a pretty standard way of rounding, because if 5 is always rounded up, this can weight things like averages and sums higher than they should be.

嘿嘿嘿 2024-12-15 09:37:45

默认舍入为 MidpointRounding.ToEven(银行家舍入),这意味着舍入位置处的数字将倾向于偶数(即,它将移动到 3.4,因为 4 是偶数)。

这是为了最大限度地减少当所有中点舍入都朝同一方向时可能发生的错误累积(尽管这当然取决于您的输入数据 - 使用 AwayFromZeroAwayFromZero 时,正数和负数的相等混合可能没问题。代码>)。

所以你有:

Math.Round(3.44, 1); //Returns 3.4.
Math.Round(3.45, 1); //Returns 3.4 (down towards 4).
Math.Round(3.54, 1); //Returns 3.5.
Math.Round(3.55, 1); //Returns 3.6 (up towards 6).

请参阅这个回答以获取所有可用选项的详细说明。

The default rounding is MidpointRounding.ToEven (banker's rounding) which means it will gravitate towards an even number for the digit at the rounding location (i.e., it will move to 3.4 because 4 is even).

This is intended to minimise the accumulation of errors which may occur when all midpoint rounding goes in the same direction (though this of course depends on your input data - an equal mix of positive and negative numbers may be fine with AwayFromZero).

So you have:

Math.Round(3.44, 1); //Returns 3.4.
Math.Round(3.45, 1); //Returns 3.4 (down towards 4).
Math.Round(3.54, 1); //Returns 3.5.
Math.Round(3.55, 1); //Returns 3.6 (up towards 6).

See this answer for a detailed explanation of all the options available to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文