Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsNegative(double) ?

Status
Not open for further replies.

Funkymatt

Programmer
Aug 27, 2002
101
US
Is there an IsNegative function of any sort? If no, what is the best way to determine a doubles positive or negative value?

I suppose I could always cast to a string and then read the leftmost character. If it's "-" then negative and if not, it's positive (assuming leading zeros trimmed). But I think there has to be a better way? Anyone?
 
this may answer your question.

thread269-580099

~za~
You can't bring back a dead thread!
 
Sounds like you are getting bogged down in the use of the Double object vs the double primitive.

If you are using the Double object then compareTo() is worth a look for your use.

See if this snippet helps shed light :

public class Scratcher
{
public Scratcher()
{
}

public void cycle1()
{
double d1 = 56D;
double d2 = -546D;
double d3 = 0D;

Double D1 = new Double(d1);
Double D2 = new Double(d2);
Double D3 = new Double(d3);

printDouble(d1);
printDouble(d2);
printDouble(d3);

printDouble(D1);
printDouble(D2);
printDouble(D3);
}

public void printDouble(double d)
{
if (d > 0D)
System.out.println(d + " is > 0");
else if (d < 0D)
System.out.println(d + &quot; is < 0&quot;);
else if (d == 0D)
System.out.println(d + &quot; is == 0&quot;);
}

public void printDouble(Double d)
{
if (d.compareTo(new Double(0D)) > 0)
System.out.println(d.toString() + &quot; is > 0&quot;);
else if (d.compareTo(new Double(0D)) < 0)
System.out.println(d.toString() + &quot; is < 0&quot;);
else if (d.compareTo(new Double(0D)) == 0D)
System.out.println(d.toString() + &quot; is == 0&quot;);
}
public static final void main(String[] args)
{
Scratcher me = new Scratcher();
me.cycle1();
}//eom

}//eoc

Will give as output :

56.0 is > 0
-546.0 is < 0
0.0 is == 0
56.0 is > 0
-546.0 is < 0
0.0 is == 0


RjB.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top