3 回答
TA贡献1847条经验 获得超7个赞
将您的评分乘以2,然后使用舍入Math.Round(rating, MidpointRounding.AwayFromZero),然后将该值除以2。
Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2
TA贡献1821条经验 获得超4个赞
这是我编写的几种方法,它们总是可以向上或向下舍入为任何值。
public static Double RoundUpToNearest(Double passednumber, Double roundto)
{
// 105.5 up to nearest 1 = 106
// 105.5 up to nearest 10 = 110
// 105.5 up to nearest 7 = 112
// 105.5 up to nearest 100 = 200
// 105.5 up to nearest 0.2 = 105.6
// 105.5 up to nearest 0.3 = 105.6
//if no rounto then just pass original number back
if (roundto == 0)
{
return passednumber;
}
else
{
return Math.Ceiling(passednumber / roundto) * roundto;
}
}
public static Double RoundDownToNearest(Double passednumber, Double roundto)
{
// 105.5 down to nearest 1 = 105
// 105.5 down to nearest 10 = 100
// 105.5 down to nearest 7 = 105
// 105.5 down to nearest 100 = 100
// 105.5 down to nearest 0.2 = 105.4
// 105.5 down to nearest 0.3 = 105.3
//if no rounto then just pass original number back
if (roundto == 0)
{
return passednumber;
}
else
{
return Math.Floor(passednumber / roundto) * roundto;
}
}
- 3 回答
- 0 关注
- 1446 浏览
添加回答
举报