我必须显示评级,为此我需要增加如下:
如果数字是1.0则应该等于1
如果数字是1.1应该等于1
如果数字是1.2应该等于1
如果数字是1.3应该等于1.5
如果数字是1.4应该等于1.5 如果数字是1.4应该等于1.5
如果数字是1.5应该等于1.5
如果数字是1.6应该等于1.5
如果数字是1.7应该等于1.5
如果数字是1.8应该等于2.0
如果数字是1.8应该等于2.0 如果数字是1.9应该等于2.0
如果数字是2.0应该等于2.0
如果数字是2.1应该等于2.0
等等......
有没有一种简单的方法来计算所需的值?
Joh*_*sch 192
将您的评分乘以2,然后使用舍入Math.Round(rating, MidpointRounding.AwayFromZero),然后将该值除以2.
Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2
NER*_*808 15
以下是我编写的几种方法,它们总是向上或向下舍入到任何值.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
105549 次 |
| 最近记录: |