English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can you write the code for me so that the x=true if y is an integer and x=false if y isn't an integer? Please make sure this will work with very large y values (When I used "\", as part of the code, if the number got too high, it didn't work)

Here is the code to start with.
dim x as boolean
dim y

2007-02-11 10:55:19 · 6 answers · asked by Matzah Boy 4 in Computers & Internet Programming & Design

You can either answer the question above, or you can solve the following:

x and y are numbers.
Make the program figure out whether x/y is an integer.

2007-02-11 11:10:11 · update #1

I use vb.net

2007-02-11 13:03:53 · update #2

6 answers

The previous answer won't solve your request.

There are a few ways around this. Unfortunately,you don't indicate if you are using VB 6 or VB.NET, so I'm doing this in VB.NET. Specifically, I'll just use a regular expression to see if the only thing in Y is numerals.

Imports System.Text.RegularExpressions

Dim X As Boolean, Y As String
Dim reNumber As New Regex("^[0-9]{1,}$")
If Not reNumber.IsMatch(Y) Then
X = False
Else
X = True
End If

The regex pattern ^[0-9]{1,}$ means, "from the start of the string (^) to the end of the string ($), if there is at least one ({1,}) integer, and integers only ([0-9]). Thus, we will get true from IsMatch only if there is at least one integer, and nothing but integers, in Y.

To solve the actual problem you want, we can use modulus. Modulus gives us the remainder of a division; if it is 0, the product of x / y is an integer:

Dim X As Integer, Y As Integer, IsAnInteger As Boolean

If Y > 0 Then ' you can't divide by 0!
If X Mod Y = 0 Then
IsAnInteger = True
End If
Else
IsAnInteger = False
End If

UPDATE:

Jeremy and steve.c_50 both use improper methods for Visual Basic.

There is no Math.floor in VB (Jeremy).

Also, you explicitly recast variable types in VB with CType, not (int). Further, either method you use would simply return an integer, so you would always get a true condition (steve.c_50).

2007-02-11 11:39:25 · answer #1 · answered by Anonymous · 1 0

Using VB '05

Dim x as Boolean
Dim y as Short
If Int(y)=y then
x=True
Else
x=False
End If

2007-02-18 16:39:57 · answer #2 · answered by _anonymous_ 4 · 1 1

dim x as boolean.
dim y as single
if (y - Math.Floor(y)) < 0.0001 then
x = true
else
x = false
end if

Since floating point math isn't exact, you can't check to see if it is the same exactly, but you can check if it's very very close, and then you can assume that it's an integer.

2007-02-11 11:48:46 · answer #3 · answered by Anonymous · 1 2

I'll take a shot. 20 different ways to do this.

Dim Y as variant ' I think those are big numbers..... .:)
dim x as boolean


if Y=int(Y) then
x=true
else
x=false
end if

2007-02-11 11:56:25 · answer #4 · answered by steve.c_50 6 · 2 1

dim x as boolean
dim y
y = FALSE

dim z
z= y- fix(y)

if z = 0 then
y = TRUE
endif

2007-02-11 11:08:37 · answer #5 · answered by ⊂( ゚ ヮ゚)⊃ 4 · 2 0

check out http://www.pscode.com for great sample codes. Since you didn't mention what version of VB you're using (.NET or previous) I can't be of any more help than that.

2007-02-11 13:02:27 · answer #6 · answered by Richard H 7 · 0 2

fedest.com, questions and answers