有没有办法根据值是大于 0.5 还是小于 0.5 来进行下限/上限?

发布于 2024-12-11 10:05:00 字数 183 浏览 0 评论 0原文

我正在尝试对我的值进行四舍五入,以便如果它是 0.5 或更大,它就会变成 1,否则它就会变成 0。例如:

3.7 -> 4;
1.3 -> 1;
2.5 -> 3;
...

有什么想法吗?

I am trying to round my values so that if it's 0.5 or greater, it becomes 1, else it becomes 0. For example:

3.7 -> 4;
1.3 -> 1;
2.5 -> 3;
...

Any ideas?

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

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

发布评论

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

评论(4

何其悲哀 2024-12-18 10:05:00
Math.Round(3.7,MidpointRounding.AwayFromZero);

http://msdn.microsoft.com/en-us/library/system .midpointrounding.aspx

在上面,我使用了AwayFromZero进行四舍五入,因为默认是银行家四舍五入,所以如果分数是0.5,它会四舍五入到最接近的 甚至。所以 3.5 变成 4(最接近的偶数),但 2.5 变成 2(最接近的偶数)。因此,您选择了如上所示的不同方法来将 3.5 变为 4 和 2.5 变为 3。

Math.Round(3.7,MidpointRounding.AwayFromZero);

http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx

In the above, I made use of AwayFromZero for rounding because the default is Banker's rounding, so if the fraction is 0.5, it is rounded to nearest even. So 3.5 becomes 4 (nearest even), but 2.5 becomes 2 (nearest even). So you choose a different method as shown above to make 3.5 to 4 and 2.5 to 3.

桜花祭 2024-12-18 10:05:00

最简单的方法是向输入添加 0.5,然后转换为 int。

The simplest way is add 0.5 to the input, then cast to int.

追星践月 2024-12-18 10:05:00

我是最后一个到达的,所以我会讲一些不同的事情。您可以通过不使用 double0.5 舍入为 1!使用小数double 不适合拥有“精确”的数字。

启动这段代码并享受乐趣(请注意,存在/曾经有一个“bug”在单声道中,像 0.49999999999999994 这样的数字,所以要在 ideone 上运行它,我必须稍微修改它以尝试舍入 1.5:http://ideone.com/57XAYV)

public static void Main()
{
    double d = 1.0;
    d -= 0.3;
    d -= 0.2;

    Console.WriteLine("Standard formatting: {0}", d); // 0.5
    Console.WriteLine("Internal Representation: {0:r}", d); // 0.49999999999999994
    Console.WriteLine("Console WriteLine 0 decimals: {0:0}", d); // 1
    Console.WriteLine("0 decimals Math.Round: {0}", Math.Round(d, MidpointRounding.AwayFromZero)); // 0
    Console.WriteLine("15 decimals then 0 decimals Math.Round: {0}", Math.Round(Math.Round(d, 15, MidpointRounding.AwayFromZero), MidpointRounding.AwayFromZero)); // 1
}

I arrived last, so I'll tell something different. You round 0.5 to 1 by not using double! Use decimals. double aren't good to have "exact" numbers.

Launch this piece of code and have fun (note that there is/was a "bug" in mono on numbers like 0.49999999999999994, so to run it on ideone I had to modify it a little to try to round 1.5: http://ideone.com/57XAYV)

public static void Main()
{
    double d = 1.0;
    d -= 0.3;
    d -= 0.2;

    Console.WriteLine("Standard formatting: {0}", d); // 0.5
    Console.WriteLine("Internal Representation: {0:r}", d); // 0.49999999999999994
    Console.WriteLine("Console WriteLine 0 decimals: {0:0}", d); // 1
    Console.WriteLine("0 decimals Math.Round: {0}", Math.Round(d, MidpointRounding.AwayFromZero)); // 0
    Console.WriteLine("15 decimals then 0 decimals Math.Round: {0}", Math.Round(Math.Round(d, 15, MidpointRounding.AwayFromZero), MidpointRounding.AwayFromZero)); // 1
}
∞觅青森が 2024-12-18 10:05:00

向上舍入

Math.Round(3.5, 0, MidpointRounding.AwayFromZero) -> 4

向下舍入

Math.Round(3.5, 0, MidpointRounding.ToEven) -> 3

Round-Up

Math.Round(3.5, 0, MidpointRounding.AwayFromZero) -> 4

Round Down

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