我试图将双精度值乘以 -1 以获得负值。它继续给我一个积极的价值
编辑:我把所有的代码都放了。
公共类 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;
}
}
打印的结果是
13.93 - 减 - 2.0
这是正确的,除了 2.0 应该是 -2.0