Negative number

T

TJF

How can I recognize (in VB) a number is negative?

num1 = 5
num2 = 7
num3 = -4

If numx = "negative" then
......



Thanks Tom
 
R

Rob van Gelder

Sgn(x) returns -1 when x is negative

1: Greater than zero
0: Equal to zero
-1: Less than zero
 
T

TJF

Thats it.

But there no way to use it like this:

If Sgn(num1) = -1 Or Sgn(num2) = -1 then
.....

only way is
If Sgn(num1) = -1 Then ....
If Sgn(num2) = -1 Then ....

thats terible
 
B

Bob Phillips

This works fine

If Sgn(-1) = -1 Or Sgn(-2) - 1 Then
MsgBox "negatives"
End If


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
F

Frank Kabel

Hi
try
If (Sgn(num1) = -1) Or (Sgn(num2) = -1) then
....
end if

should work
Frank
 
T

TJF

Looks like I have problem with data declarations.

Do you know how can I change data declaration?
I know this

new = CDbl(old)

but I need something like this:

old = CDbl(old)
....but this of course dosnt work

is there something like this

old = set as double
 
F

Frank Kabel

Hi
AFAIK it is not possible to change the variable's data type during
runtime

Frank
 
B

Bob Phillips

Declare old as Double, and Cast the variable when assigned, something like

old = CDbl(Range("A1").Value)

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
T

TJF

What a shame
My problem is I have numx as Textbox and then this:

If Sgn(num1) = -1 Or Sgn(num2) = -1 then

dosnt work so only way for me is:

num1_new = CLng(num1)
num2_new = CLng(num2)

If Sgn(num1_new) = -1 Or Sgn(num2_new) = -1 then

is that?
 
B

Bob Phillips

How about

Sgn(CDbl(TextBox1.Text))

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
B

Bob Phillips

It worked for me, so that means that I probably don't fully understand your
problem. Sorry.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
R

Rob van Gelder

That should work. What is the content of TextBox1.Text? From the sounds of
things, it's not a number.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top