嘿傢伙,我怎麼整數小數到精確到0.05分在C#?將小數舍入到最接近的0.05?
e.g $ 3.44,雖然還沒有想通了這一點要四捨五入至$ 3.45或$爲3.48至$ 3.50
我math.round發揮各地()。
嘿傢伙,我怎麼整數小數到精確到0.05分在C#?將小數舍入到最接近的0.05?
e.g $ 3.44,雖然還沒有想通了這一點要四捨五入至$ 3.45或$爲3.48至$ 3.50
我math.round發揮各地()。
這裏有幾個方法,我寫了,將永遠或圓形上下爲任意值。
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;
}
}
這個片段只是一輪上漲到最近的0.05
public static decimal Round(decimal value) {
var ceiling = Math.Ceiling(value * 20);
if (ceiling == 0) {
return 0;
}
return ceiling/20;
}
爲回合下來,用'Math.Floor(VAL * 20),不管有多少次問題已經被問/ 20' – Timeless 2015-09-15 10:59:41
,你永遠不應該疏遠這樣的人。我低調了。 – Krythic 2016-08-21 17:31:09