2010-10-11 62 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; 
}