简体   繁体   English

vb.net中的Math.Round无法按预期工作

[英]Math.Round in vb.net not working as expected

I have this number : 666872700 i need to round it to be : 666900000 我有这个数字: 666872700我需要四舍五入为: 666900000

I used : Math.Round(666872700,4) but this is not working Is there any easy way in vb.net I can use other than dividing by 100000 then rounding then multiplying by 100000 ? 我用过: Math.Round(666872700,4)但是这不起作用vb.net中有什么简单的方法可以使用,除了除以100000然后四舍五入,再乘以100000

The documentation for Math.Round clearly states: Math.Round文档明确指出:

Rounds a double-precision floating-point value to a specified number of fractional digits . 将双精度浮点值四舍五入为指定数量的小数位数

So it rounds stuff behind the decimal separator, but not integral part. 因此,它会将小数点后的内容四舍五入,而不是整数部分。 I know of no other way than dividing, rounding and then multiplying again. 除了分割,四舍五入然后再相乘,我没有其他办法。


If you know a little C#, you can use the following extension method Jason Larke wrote in his answer to this question . 如果您了解一点C#,则可以使用Jason Larke在回答这个问题时写的以下扩展方法。 I don't know whether it works, but you should be able to translate it to VB.NET and try it: 我不知道它是否有效,但是您应该可以将其转换为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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM