C# 中小数点四舍五入到最接近的四分之一

发布于 2024-09-01 13:15:20 字数 112 浏览 4 评论 0原文

c# 中是否有一种简单的方法将小数四舍五入到最近的四分之一 iex0, x.25, x.50 x.75 例如 0.21 将四舍五入为 0.25,5.03 将四舍五入为 5.0

提前感谢您的帮助。

Is there a simple way in c# to round a decimal to the nearest quarter i.e. x.0, x.25, x.50 x.75
for example 0.21 would round to 0.25, 5.03 would round to 5.0

Thanks in advance for any help.

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

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

发布评论

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

评论(4

你げ笑在眉眼 2024-09-08 13:15:21

将其乘以四,根据需要将其四舍五入为整数,然后再次除以四:

x = Math.Round (x * 4, MidpointRounding.ToEven) / 4;

舍入的各种选项及其解释,可以在这个优秀的答案中找到 此处 :-)

Multiply it by four, round it as you need to an integer, then divide it by four again:

x = Math.Round (x * 4, MidpointRounding.ToEven) / 4;

The various options for rounding, and their explanations, can be found in this excellent answer here :-)

清醇 2024-09-08 13:15:21

或者,您可以使用本博客中给出的 UltimateRoundingFunction:
http://rajputyh.blogspot.in/2014/09/ the-ultimate-rounding-function.html

//amountToRound =>;输入金额
//最近的=>如果四舍五入为 0.25,四舍五入为 1 美分则为 0.01,四舍五入为 1 美元则为 1
//公平=> 0 到 0.9999999___ 之间。
// 0 表示下限,0.99999... 表示上限。但对于天花板,我会推荐 Math.Ceiling
// 0.5 = 标准舍入函数。它会将边界情况四舍五入。即 1.5 比 2,而不是 1。
// 0.4999999...非标准舍入函数。边界大小写向下舍入。即 1.5 比 1,而不是 2。
// 0.75 表示前 75% 的值将向下舍入,其余 25% 的值将向上舍入。
小数 UltimateRoundingFunction(小数 amountToRound、小数 nearstOf、小数公平性)
{
    return Math.Floor(amountToRound/nearstOf+公平性)*nearstOf;
}

请拨打以下电话进行标准舍入。即 1.125 将四舍五入为 1.25

UltimateRoundingFunction(amountToRound, 0.25m, 0.5m);

调用下面的方法来四舍五入边界值。即 1.125 将四舍五入为 1.00

UltimateRoundingFunction(amountToRound, 0.25m, 0.4999999999999999m);

所谓的“银行家舍入”对于 UltimateRoundingFunction 是不可能的,您必须使用 paxdiablo 的答案来获得该支持:)

Alternatively, you can use UltimateRoundingFunction given in this blog:
http://rajputyh.blogspot.in/2014/09/the-ultimate-rounding-function.html

//amountToRound => input amount
//nearestOf => .25 if round to quater, 0.01 for rounding to 1 cent, 1 for rounding to $1
//fairness => btween 0 to 0.9999999___.
//            0 means floor and 0.99999... means ceiling. But for ceiling, I would recommend, Math.Ceiling
//            0.5 = Standard Rounding function. It will round up the border case. i.e. 1.5 to 2 and not 1.
//            0.4999999... non-standard rounding function. Where border case is rounded down. i.e. 1.5 to 1 and not 2.
//            0.75 means first 75% values will be rounded down, rest 25% value will be rounded up.
decimal UltimateRoundingFunction(decimal amountToRound, decimal nearstOf, decimal fairness)
{
    return Math.Floor(amountToRound / nearstOf + fairness) * nearstOf;
}

Call below for standard rounding. i.e. 1.125 will be rounded to 1.25

UltimateRoundingFunction(amountToRound, 0.25m, 0.5m);

Call below for rounding down border values. i.e. 1.125 will be rounded to 1.00

UltimateRoundingFunction(amountToRound, 0.25m, 0.4999999999999999m);

So called "Banker's Rounding" is not possible with UltimateRoundingFunction, you have to go with paxdiablo's answer for that support :)

久而酒知 2024-09-08 13:15:21

注意:这不是您在脚本中执行的操作。

如果您有这样的情况:

    this.transform.position = new Vector3(
    x: Mathf.Round(this.transform.position.x) + direction.x,
    y: Mathf.Round(this.transform.position.y) + direction.y,
    z: 0.0f
    );

然后转到 Unity >项目设置>时间
然后你可以改变时间。

Notice: This isn't a thing you do in a script.

If you have something like this:

    this.transform.position = new Vector3(
    x: Mathf.Round(this.transform.position.x) + direction.x,
    y: Mathf.Round(this.transform.position.y) + direction.y,
    z: 0.0f
    );

Then go to Unity > Project Settings > Time
And then you can change the time.

思念绕指尖 2024-09-08 13:15:21

基于此答案的扩展方法。

namespace j
{
    public static class MathHelpers
    {
        public static decimal RoundToNearestQuarter(this decimal x)
        {
            return Math.Round(x * 4, MidpointRounding.ToEven) / 4;
        }
    }
}

An extension method based on this answer.

namespace j
{
    public static class MathHelpers
    {
        public static decimal RoundToNearestQuarter(this decimal x)
        {
            return Math.Round(x * 4, MidpointRounding.ToEven) / 4;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文