Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

New to JAVA and Frustrated with the math

  Asked By: Harry    Date: Apr 15    Category: Java    Views: 712
  

Big time mainframe background and am trying to learn JAVA on my own.
So here is my simple question...

I am trying to create test data and insert into my DB2 database. Why
doesn't the math work here.

double myNumber = 0;

if (myNumber < .15){
myNumber =+ .0001;
}else{
myNumber = .04;
}

when I insert into the database, myNumber is always the same value
of .0001? This seems like simple logic but it is making me go nuts.
Should I be using the BigDecimal to get this to work?

What simple concept am I missing.... Please be easy on the old guy...

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Amir Shaikh     Answered On: Apr 15

It looks like you have the operands out of order.

Change "=+" to "+=" and it should owrk just fine.

Your code:

double myNumber = 0;

if (myNumber < .15){
myNumber =+ .0001;
}else{
myNumber = .04;
}

Should read:
double myNumber = 0;

if (myNumber < .15){
myNumber += .0001;
}else{
myNumber = .04;
}

 
Answer #2    Answered By: Abriana Rossi     Answered On: Apr 15

myNumber += .0001;............................

 
Answer #3    Answered By: Mario Ryan     Answered On: Apr 15

Man I should fully wake up before reading stuff

 
Answer #4    Answered By: Garry Sanchez     Answered On: Apr 15

Would that be because you have set myNumber to always equal 0 hence my
number is always going to be < .15?

 
Answer #5    Answered By: Chung Tran     Answered On: Apr 15

I figured it out... jeeze this JAVA is picky... the fix is to use

+= not =+

there is a big  difference in the result.

 
Answer #6    Answered By: Salvador Alexander     Answered On: Apr 15

Wait until you mix up "=" and "==".

Then, you'll have some real fun trying to find the error.

 
Didn't find what you were looking for? Find more on New to JAVA and Frustrated with the math Or get search suggestion and latest updates.




Tagged: