2010-10-11 63 views
2

我需要將數字舍入到0小數(對於分頁系統)。C# - 小數點後的整數

我已經嘗試過這樣的事情:

Math.Round(double1, 0, MidpointRounding.AwayFromZero); 

如果double1是7,2或7,6我也需要一輪8,但我沒有得到這一點。

有人可以幫我嗎?

感謝

回答

9

使用Math.Ceiling總是四捨五入到下一個整數:

double roundUp = Math.Ceiling(double1); 
+0

謝謝,這是我所尋找的。 – 2010-10-11 16:17:38

1

如果你不想使用Math.Ceiling,出於某種原因 - 你可以這樣做:

public int Ceil(double x) { 
    return (int) x + ((int) x < x ? 1 : 0); 
} 
0

在您的情況下..使用p = 1 ..

float RoundTo(float x, float p) 
{ 
    float y = 1/p; 
    return int((x+(1/(y+y)))*y)/y; 
} 

float RoundUp(float x, float p) 
{ 
    float y = 1/p; 
    return int((x+(1/y))*y)/y; 
} 

float RoundDown(float x, float p) 
{ 
    float y = 1/p; 
    return int(x*y)/y; 
}