vb.net 检查一个数字是否可以被另一个数字整除的快速方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4985846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:17:21  来源:igfitidea点击:

Fast way to check if a number is evenly divisible by another?

vb.netnumbersintegerdivision

提问by pimvdb

I was wondering what the fastest way is to check for divisibility in VB.NET.

我想知道在 VB.NET 中检查可分性的最快方法是什么。

I tried the following two functions, but I feel as if there are more efficient techniques.

我尝试了以下两个功能,但感觉好像有更高效的技巧。

Function isDivisible(x As Integer, d As Integer) As Boolean
     Return Math.floor(x / d) = x / d
End Function

Another one I came up with:

我想出了另一个:

Function isDivisible(x As Integer, d As Integer) As Boolean
     Dim v = x / d
     Dim w As Integer = v
     Return v = w
End Function

Is this a more practical way?

这是更实用的方法吗?

回答by gor

Use Mod:

使用Mod

Function isDivisible(x As Integer, d As Integer) As Boolean
    Return (x Mod d) = 0
End Function

回答by Matt F

Use 'Mod' which returns the remainder of number1 divided by number2. So if remainder is zero then number1 is divisible by number2.

使用 'Mod' 返回 number1 除以 number2 的余数。因此,如果余数为零,则 number1 可被 number2 整除。

e.g.

例如

Dim result As Integer = 10 Mod 5 ' result = 0

Dim 结果为整数 = 10 Mod 5 ' 结果 = 0

回答by kefeizhou