vb.net中的Math.Round没有按预期工作

时间:2016-07-14 08:53:34

标签: vb.net rounding

我有这个号码:clearInterval我需要将其舍入为:666872700

我使用过:666900000但这不起作用 在vb.net中是否有任何简单的方法我可以使用除Math.Round(666872700,4)之外的其他方式然后舍入然后乘以100000

1 个答案:

答案 0 :(得分:0)

Math.Round的{​​{3}}明确指出:

  

将双精度浮点值舍入为指定数量的小数位数

所以它将小数分隔符后面的东西舍入,但不是整数部分。我知道别的方法除了分割,舍入然后再乘以。

如果你知道一点C#,你可以使用以下扩展方法Jason Larke在他对documentation的回答中写道。我不知道它是否有效,但您应该能够将其翻译成VB.NET并尝试:

public static class MathExtensions
{
    public static int Round(this int i, int nearest)
    {
        if (nearest <= 0 || nearest % 10 != 0)
            throw new ArgumentOutOfRangeException("nearest", "Must round to a positive multiple of 10");

        return (i + 5 * nearest / 10) / nearest * nearest;
    }
}
相关问题