I am trying to multiply a double value by -1 to get the negative value. It continues to give me a positive value
EDIT: I am putting all the code up.
public class DecToTime {
public static void main(String[] args) throws IOException {
DecToTime dtt = new DecToTime();
double val = dtt.getNumber("13.930000000000E+02");
System.out.println(val);
}
public double getNumber(String number) throws IOException{
StringReader reader = new StringReader(number);
int c;
String mantissa="";
String sign="";
String exponent="";
boolean isMantissa=true;
while((c = reader.read()) != -1) {
char character = (char) c;
if(character=='E'){
isMantissa=false;
}
if(isMantissa==true){
mantissa=mantissa+character;
}
if(isMantissa==false){
if((character=='+')|| (character=='-')){
if(character=='+') {
sign = "plus";
}
if(character=='-') {
sign = "minus";
}
}
if(!(sign.equals(""))){
exponent=exponent+character;
}
}
}
System.out.println(mantissa+" - "+sign+" - "+exponent);
double man = Double.parseDouble(mantissa);
double exp;
if(sign.equals("plus")){
exp = Double.parseDouble(exponent);
}
else {
exp = Double.parseDouble(exponent);
System.out.println("Exp: "+exponent);
}
System.out.println(man+" - "+sign+" - "+exp);
double value = man*Math.pow(10, exp);
return value;
}
}
The printed result is
13.93 – minus – 2.0
which is correct except that 2.0 should be -2.0
I suspect that in the
elsebranch, the parsed value ofexpis already negative, so negating it results in the positive value you see. Try printing out its value before the negation.It would certainly help us (and you) if you showed / printed the original value of
exponentthough.