舍入到0 c#

时间:2015-06-22 10:06:44

标签: c#

我想将带有2位的十进制数舍入为整数。

  

Ex:3.772.002,47 ---十进制       3.772.003

我使用此代码但没有成功:

ssOut = Math.Round(ssIn, ssDecimal, MidpointRounding.AwayFromZero);

任何人都可以帮助我吗? 感谢。

2 个答案:

答案 0 :(得分:1)

您需要将结果转换为int

var ssOut = (int)Math.Round(ssIn,
                            0, /* zero because you want to cast to integer - no fraction part is needed */
                            MidpointRounding.AwayFromZero);

您可以对以下重载执行相同的操作:

var ssOut = (int)Math.Round(ssIn, MidpointRounding.AwayFromZero);

但看起来你需要Math.Ceiling - 就像在下面的评论中所说的那样。

答案 1 :(得分:0)

看起来你的输入可能是一个字符串。首先使用Float.Parse()或Double.Parse()在舍入之前将字符串转换为数字。