是否可以根据该值是大于0.5还是小于0.5来下限/上限? [英] Is there a way to floor/ceil based on whether the value is over 0.5 or under?

查看:166
本文介绍了是否可以根据该值是大于0.5还是小于0.5来下限/上限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试取整我的值,以便它等于或大于0.5时变为1,否则变为0.例如:

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;
...

有什么想法吗?

推荐答案

Math.Round(3.7,MidpointRounding.AwayFromZero);

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

在上面,我将AwayFromZero用于四舍五入,因为默认值是Banker的四舍五入,因此,如果小数为0.5,则将其四舍五入为最接近的偶数.因此3.5变为4(最接近偶数),而2.5变为2(最接近偶数).因此,您选择上面显示的另一种方法将3.5设为4,并将2.5设为3.

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.

这篇关于是否可以根据该值是大于0.5还是小于0.5来下限/上限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆