如何四舍五入到最接近的 0.5?
问题描述:
我必须显示评级,为此,我需要按如下方式递增:
I have to display ratings and for that, I need increments as follows:
输入 | 圆形 |
---|---|
1.0 | 1 |
1.1 | 1 |
1.2 | 1 |
1.3 | 1.5 |
1.4 | 1.5 |
1.5 | 1.5 |
1.6 | 1.5 |
1.7 | 1.5 |
1.8 | 2.0 |
1.9 | 2.0 |
2.0 | 2.0 |
2.1 | 2.0 |
等等...
是否有一种简单的方法来计算所需的值?
Is there a simple way to compute the required values?
答
将您的评分乘以 2,然后使用 Math.Round(rating, MidpointRounding.AwayFromZero)
取整,然后将该值除以 2.
Multiply your rating by 2, then round using Math.Round(rating, MidpointRounding.AwayFromZero)
, then divide that value by 2.
Math.Round(value * 2, MidpointRounding.AwayFromZero)/2