开发者

Converting Decimal To Integer

string retval;

retval = Decimal.ToInt32(1899.99M).ToString();

the output is 1899. But i 开发者_StackOverflowwant to if decimal is bigger .5 then output is 1900 else then ouput is 1899. how can i do this? thanks in advance !


Use Math.Round first.

http://msdn.microsoft.com/en-us/library/system.math.round.aspx


When using Math.Round You have two choices for rounding x.5:

Banker's round / Round to even. Avoids biases, by sometimes rounding up(1.5=>2) and sometimes rounding down(0.5=>0), used by default if you don't specify the parameter.

Int32 i=Math.Round(d, MidpointRounding.ToEven);

The rounding you learn at school, where it always rounds towards infinity (0.5=>1, -0.5=>-1)

Int32 i=Math.Round(d, MidpointRounding.AwayFromZero);


Try something like :

Double d = 1899.99;
Int32 i = Math.Round(d);
String retval = i.ToString(CultureInfo.InvariantCulture);


Decimal.ToInt32(Math.Round(1899.99M)).ToString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜