3 回答

TA貢獻1847條經驗 獲得超7個贊
將您的評分乘以2,然后使用舍入Math.Round(rating, MidpointRounding.AwayFromZero),然后將該值除以2。
Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2

TA貢獻1821條經驗 獲得超5個贊
這是我編寫的幾種方法,它們總是可以向上或向下舍入為任何值。
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 關注
- 1486 瀏覽
添加回答
舉報